Killing an Excel process from withing a VB App

How can i make my VB prog kill an Excel process?  The code I'm using is this:

Private Declare Function WaitForSingleObject Lib "kernel32" _
   (ByVal hHandle As Long, _
   ByVal dwMilliseconds As Long) As Long

Private Declare Function FindWindow Lib "user32" _
   Alias "FindWindowA" _
   (ByVal lpClassName As String, _
   ByVal lpWindowName As String) As Long

Private Declare Function PostMessage Lib "user32" _
   Alias "PostMessageA" _
   (ByVal hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   ByVal lParam As Long) As Long

Private Declare Function IsWindow Lib "user32" _
   (ByVal hwnd As Long) As Long

'Constants used by the API functions
Const WM_CLOSE = &H10
Const INFINITE = &HFFFFFFFF

Public Sub CleanUp()
    Dim hWindow As Long
    Dim lngResult As Long
    Dim lngReturnValue As Long
   
    hWindow = FindWindow(vbNullString, "EXCEL")
    lngReturnValue = PostMessage(hWindow, WM_CLOSE, vbNull, vbNull)
    lngResult = WaitForSingleObject(hWindow, INFINITE)

    'Does the handle still exist?
    DoEvents
    hWindow = FindWindow(vbNullString, "EXCEL")
    If IsWindow(hWindow) = 1 Then
       'The handle still exists. Use the TerminateProcess function
       'to close all related processes to this handle
       MsgBox "Handle still exists."
    Else
       'Handle does not exist.
       MsgBox "Program closed."
    End If
End Sub

Unfortunately this uses the FindWindow function which doesn't seem to work. If anyone can provide code for using the ExitProcess or TerminateProcess functions to remove Excel.exe from the task list, I would be very grateful.
wstephAsked:
Who is Participating?
 
ATMConnect With a Mentor Commented:
I haven't understood U try to kill process but seek for windoz??? Win32 API have more applicatable functions for that:
----------------NO COMMENTS-------------
Process Walking
A snapshot that includes the process list contains information about each currently executing process. You can retrieve information for the first process in the list by using the Process32First function. After retrieving the first process in the list, you can traverse the process list for subsequent entries by using the Process32Next function. Process32First and Process32Next fill a PROCESSENTRY32 structure with information about a process in the snapshot.

You can retrieve an extended error status code for Process32First and Process32Next by using the GetLastError function.

You can read the memory in a specific process into a buffer by using the Toolhelp32ReadProcessMemory function (or the VirtualQueryEx function).

Note  The contents of the th32ProcessID and th32ParentProcessID members of PROCESSENTRY32 are process identifiers and can be used by Win32 Application Programming Interface (API) elements.
----------------NO COMMENTS-------------

in struct PROCESSENTRY32 exist
TCHAR szExeFile that's executed file name. So You can obtain handle for process itself.
0
 
AzraSoundCommented:
http://www.mvps.org/vb/samples.htm
there is a sample there called findpart.zip that shows how to find an open window given part of its name.  with findwindow often it doesnt work because we do not know exact window text of the window
0
 
AzraSoundCommented:
type Microsoft Excel instead of EXCEL...it may do the trick too
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
wstephAuthor Commented:
Forgot to mention, I'm using GetObject and CreateObject to run the Excel.exe like so:

Set appExcel = GetObject(, "Excel.Application") 'look for a running copy of Excel
    If Err.Number <> 0 Then 'If Excel is not running then
        Set appExcel = CreateObject("Excel.Application") 'run it
    End If
0
 
AzraSoundCommented:
Set appExcel = Nothing


0
 
Erick37Commented:
appExcel.Quit
Set appExcel = Nothing
0
 
zswenjinCommented:
Try below codes
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const WM_QUIT = &H12
Sub TerminateExcel()

Dim lpClassName As String
Dim iHwnd As Long
Dim iReturn As Long

iHwnd = FindWindow(vbNullString, "Microsoft Excel")

If iHwnd Then
    iReturn = PostMessage(iHwnd, WM_QUIT, 0&, 0&)
End If

End Sub

0
 
wstephAuthor Commented:
Unfortunately none of these examples work.  Is there anyway to just use TerminateProcess in this code to simulate using Alt+Ctrl+Delete to terminate the Excel process?
0
 
AzraSoundCommented:
You need to use the constant:
Public Const WM_CLOSE = &H10

and call it like this:
hWindow = FindWindow(vbNullString, "Microsoft Excel")
lngReturnValue = PostMessage(hWindow, WM_CLOSE, 0&, 0&)
   


0
 
AzraSoundCommented:
try it using just those two function calls and not the waitforsingleobject and iswindow.  just use those two lines to see if it closes an instance of Excel. Note the two changes from your code

"EXCEL" to "Microsoft Excel" and vbNull to 0&
0
 
wstephAuthor Commented:
Still no go.  When I do alt+ctrl+delete, I see that Excel is listed as the process still running not Microsoft Excel.  Even when I change the argument to FindWindow to just Excel, the process is still running.
0
 
AzraSoundCommented:
if its closing the window but not removing Excel from the task list then that leads me to believe that you arent closing out all of the objects you created.  Make sure for every object you created you close them or quit them and set them to nothing.  i'll look into that terminateprocess just in case.
0
 
wstephAuthor Commented:
Thanks.  Before making calls to the API functions, I'm setting the Excel application object and workbook to nothing.
0
 
AzraSoundCommented:
and when setting them to nothing you do this:

appExcel.Application.Quit
Set appExcel = Nothing

??
0
 
wstephAuthor Commented:
Nope, I'm doing this:

Set appExcel = Nothing
Set wbJohnData = Nothing
0
 
wstephAuthor Commented:
When I use the appExcel.Application.Quit statement, I get the following dialog:

Do you want to save the changes you made to 'spredsheetname.xls'?

There is no OK or Cancel button, just a Help button.  The only way out of that dialog is to alt+ctrl+delete.  Is there anyway to get rid of that?
0
 
AzraSoundCommented:
appExcel.DisplayAlerts = False

that should stop that from popping up

P.S.  Sorry for the delayed response
0
 
wstephAuthor Commented:
Thanks for the help!
0
 
AzraSoundCommented:
ummm...did i miss something?
0
 
wstephAuthor Commented:
Not at all, the last bit of code you provided worked.  Just expressing my thanks.
0
 
AzraSoundCommented:
yeah but you gave the points to someone else
0
 
AzraSoundCommented:
http://www1.experts-exchange.com/Customer_Service/Experts_Exchange/

you can post a question at customer support to rectify the situation
0
 
wstephAuthor Commented:
Sorry about that.  I don't even know who I awarded them to.  I assumed that the points would go to the last person who answered the question.  I just posted a question regarding this to community support.
0
 
darinwCommented:
Hello everyone,

AzraSound: Please see your question:

http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10339021 

darinw
Customer Service
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.