View Single Post
 
Old 05-28-2014, 04:43 PM
macropod's Avatar
macropod macropod is offline Windows 7 32bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,373
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote