This works for me:
Code:
Sub Test()
Dim gxlApp As Excel.Application
Dim gbooExcelIsRunning As Boolean
' Test whether Excel is already running.
On Error Resume Next
gbooExcelIsRunning = False ' Flag to record if we start Excel, so we can close it later.
Set gxlApp = GetObject(, "Excel.Application")
'Start Excel if it isn't running
If gxlApp Is Nothing Then
Set gxlApp = CreateObject("Excel.Application")
If gxlApp Is Nothing Then
MsgBox "Can't start Excel.", vbExclamation
Exit Sub
End If
' Record that we've started Excel.
gbooExcelIsRunning = True
End If
On Error GoTo 0
' Do stuff
If gbooExcelIsRunning = True Then gxlApp.Quit
End Sub
Even an empty instance of Excel is only terminated if the macro started it.