VB: Destroying running Excel instance (tough question ?)
Posted by Vik Kala on February 27, 2001 9:05 AM
Hello All,
This one might be a little bit tough to follow and
answer. Consider the following code that I am using before using a
CreateOject("Excel.Application") command:
Dim appExcel As Excel.Application
On Error Resume Next
Set appExcel = GetObject(, "Excel.Application")
If Not appExcel Is Nothing Then 'instance of Excel is available
msg = "An earlier instance of Excel is already running on your machine!" & vbCrLf & _
"Please close the running instance before opening or creating a " & vbCrLf & _
"new worksheet."
ans = MsgBox(msg, vbCritical + vbOKOnly, "Worksheet error")
Exit Sub
End If
The code simply checks for an already running Excel application.
If it already exists, then it informs the user and
exits the sub. Here is what the problem is. Lets say
I open an Excel spreadsheet for the very first time from
my program using CreateObject. I then close the spreadsheet
and in the "WorkbookBeforeClose" event of the Excel object,
I am executing the following code to close the running
instance of Excel:
Set xlApp = GetObject(, "Excel.Application")
If Not xlApp Is Nothing Then
xlApp.Quit
End If
PROBLEM: After closing the sheet, if I go back to my
CreateObject command, it still sees a running Excel
instance (even though I think I have destroyed it in the
event). Going to the task manager does infact show up
a running instance of Excel. Can anyone tell me how can
I destroy this running instance from within my code ?
Thanks,
Vik