Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

excel process shutdown

Status
Not open for further replies.

stonee74

Technical User
Nov 12, 2001
49
0
0
CH
Hello there,

I wrote an application using VB and OLE connections to excel.So far everything works fine. But I have problems if I close my application: an excel proces stays in the Process list of windows. This is very annoying, coz when i want to open another workbook from explorer, it hangs.
I was not able to find out, why this excel instance is not released properly on program shutdown.
Now i am looking for a way to kill this excel process.
The problem is, that this excel process does not have a name or window what i could refer to!
Any ideas how to shutdown this process anyway?

Any help is greatly appreciated

Thanks stonee
 
in your app, do you issue a .Quit call to the Excel OLE object? and then set the OLE object to Nothing? if you don't set it to nothing, it will not go away.
 
Did anyone figure this out? I am having the same issue. I do issue a quit and set the object to nothing....

Set eapp = CreateObject("Excel.Application")
eapp.Visible = True
'open file
Set edoc = eapp.workbooks.Open(UserFile2)
Set worksheet = edoc.ActiveSheet
...
...
...
eapp.DisplayAlerts = False
eapp.Save
edoc.Close

eapp.Application.Quit

Set eapp = Nothing
Set edoc = Nothing
MsgBox "Annuit~2.xls saved
 
I have encountered the problem as well a couple of times since the original posts. I have also put together a solution that definitely cleans it up so the Excel process does not hang around.

You can use this to kill off any process or VB-initiated VBA/OLE app - just pass the procedure the hWnd of the process.

Enjoy.

Code:
Public Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Public Const PROCESS_ALL_ACCESS = &H1F0FFF


Public Sub KillExcel(hWindow As Long)

  '--- Kills a running Excel process


  Dim lngRtn As Long
  Dim lngProc As Long
  Dim lngProcID As Long

  Dim hThread As Long
  
 
  If (IsWindow(hWindow) <> 0) Then
    hThread = GetWindowThreadProcessId(hWindow, lngProcID)
  
    If (lngProcID <> 0) Then
      Call App.LogEvent("Warning...killing orphan Excel process...")
  
      lngProc = OpenProcess(PROCESS_ALL_ACCESS, CLng(0), lngProcID)
      lngRtn = TerminateProcess(lngProc, CLng(0))
      Call CloseHandle(lngProcID)
    End If
  
  End If
  
End Sub


' then, in your code that creates/exits the excel process...
Public Sub ExcelProcess()
Dim oExcel As Excel.Application
Dim hExcel As Long

  Set oExcel = New Excel.Application
  hExcel = oExcel.hwnd        ' save the window handle

  ' do your thing...
  .
  .
  .

  ' now clean up
  If (Not (oExcel Is Nothing)) Then
    oExcel.Quit
    Set oExcel = Nothing
  End If
  
  ' just to be safe, check if the app is gone, kill if necessary
  Call KillExcel(hExcel)

End Sub
 
Thanks for the response. I was able to debug my code to find that excel would stay open due to a format I was placing on some cells. If i didnt run this format, the xls would close. Thanks again...
 
I had this problem on a web server where I was using an ActiveX DLL to manipulate Excel. After about a week the server got really slow and the process list was full of copies of Excel. I changed the code to kill it with the API because the .Quit wasn't doing it for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top