MrExcel Publishing
Your One Stop for Excel Tips & Solutions

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
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 ?

Posted by David Hawley on February 27, 2001 3:57 PM

Hi Vik

I'm shooting from the hip here, so I may be off the mark. But you Before Close may not be firing.

Try this

Set xlApp = GetObject(, "Excel.Application")
If Not xlApp Is Nothing Then
With xlApp
.RunAutoMacros xlAutoClose
End with
End If

Although I think this may only apply to Auto_Open and close macros. If this doesn't work then I would say you have more than one instance of Excel running ?


  • OzGrid Business Applications

Posted by yash on March 30, 2001 10:04 PM

pl let me know how to convert rupees into lacs. eg. Rs. 126560 RS. 1.27