Killing an open instance of EXCEL from VB6 - HELP

  • Thread starter Thread starter Cap Kirk
  • Start date Start date
C

Cap Kirk

I am developing a program en VB6 that extracts data from ACCESS and fills in
an EXCEL. While developing it, if the program crashes, it leaves behind an
open instance of EXCEL, which then I have to kill by hand, else the program
does not run.

Since it is at development and debugging time, I need to know how to detect
if there is an open instance of EXCEL and kill it, while loading the VB6
form. There will ALWAYS be only one instance of EXCEL open. So I can correct
the error causing the crash and just restart the program.

Any help will be welcome. Tnk You in advance
 
See if the following works. I grabbed it from something I did a while back
and cobbled it together but I've not tested it.

Sub KillXL()

Dim IsAppRunning as boolean
Dim o As Object
Dim xl as Object

Set o = GetObject("WinMgmts:").ExecQuery( _
"Select * From Win32_Process WHERE Name='" & _
AppName & "'")
If o.Count > 0 Then IsAppRunning = True

If IsAppRunning Then
Set xl = GetObject(, "Excel)
xl.Quit
End If
Set o = Nothing
Set xl = Nothing

End Sub
 
It works!
Only thing is instead of :
Set xl = GetObject(, "Excel)
you must use
Set xl = Getobject(, "Excel.Application")
I think you just mistyped it.

Thank you very much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top