
More problems with Access/Excel interoperability
Hello!
I create an instance of Excel97 from Access97, run some Excel code and
attempt closing the automation object. Sometimes Excel will terminate
cleanly, on occasion Excel will keep{*filter*} around after a .Quit statement
and not return control to Access. To work around, I decided to harness some
API code from The Access Web and MSKB to terminate any lingering instances
of Excel.
Posted below is a code sample from the Main routine - where I observed the
problem. Also, I have posted a TestCloseApp routine which creates an Excel
instance, and calls fCloseApp (an API function from the Access Web) to
terminate the instance just created, and the result: Excel still hangs
around after fCloseApp.
So what am I missing?
I am reluctantly messing around with the TerminateProcess API function.
Thank you.
Note: I have left out the API declarations and procedures borrowed from
postings on the Access Web - API page.
Sub Main()
Dim oExcel as Object
'Use fIsAppRunning from The Access Web - Dev Ashish
If fIsAppRunning("excel") then
Set oExcel=GetObject(,"excel.application")
Else
Set oExcel=CreateObject("excel.application")
End If
With oExcel
'Pass parameters
'Execute Excel procedure
End With
oExcel.Application.Quit 'sometimes this does not close Excel
'Close Excel if an instance is{*filter*} around
If fIsAppRunning("excel") then
'Use fCloseApp from The Access Web - Dev Ashish
'This does not work! Why? See the simpler example below.
Call fCloseApp("XLMain")
End If
If apiFindWindow("XLMAIN", vbNullString) <> 0 Then
MsgBox "Excel instance still available."
End If
End Sub
'This simple example leaves an Excel instance{*filter*}. What am I missing?
Sub TestCloseApp()
Dim oApp as Object
Set oApp = CreateObject("Excel.application")
'Instead of using oApp.Application.Quit
Call fCloseApp("XLMAIN")
If apiFindWindow("XLMAIN", vbNullString) <> 0 Then
MsgBox "Excel instance still available."
End If
End Sub