Solved

Killing an Excel process from withing a VB App

Posted on 2000-04-14
24
362 Views
Last Modified: 2010-05-02
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.
0
Comment
Question by:wsteph
24 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2717545
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2717554
type Microsoft Excel instead of EXCEL...it may do the trick too
0
 

Author Comment

by:wsteph
ID: 2717601
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2717632
Set appExcel = Nothing


0
 
LVL 32

Expert Comment

by:Erick37
ID: 2717657
appExcel.Quit
Set appExcel = Nothing
0
 
LVL 1

Accepted Solution

by:
ATM earned 100 total points
ID: 2717658
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
 

Expert Comment

by:zswenjin
ID: 2722437
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
 

Author Comment

by:wsteph
ID: 2729250
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2729290
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2729301
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
 

Author Comment

by:wsteph
ID: 2732922
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2732947
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:wsteph
ID: 2732969
Thanks.  Before making calls to the API functions, I'm setting the Excel application object and workbook to nothing.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2732995
and when setting them to nothing you do this:

appExcel.Application.Quit
Set appExcel = Nothing

??
0
 

Author Comment

by:wsteph
ID: 2733017
Nope, I'm doing this:

Set appExcel = Nothing
Set wbJohnData = Nothing
0
 

Author Comment

by:wsteph
ID: 2733025
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2770037
appExcel.DisplayAlerts = False

that should stop that from popping up

P.S.  Sorry for the delayed response
0
 

Author Comment

by:wsteph
ID: 2771891
Thanks for the help!
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2771896
ummm...did i miss something?
0
 

Author Comment

by:wsteph
ID: 2775797
Not at all, the last bit of code you provided worked.  Just expressing my thanks.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2775801
yeah but you gave the points to someone else
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2775803
http://www1.experts-exchange.com/Customer_Service/Experts_Exchange/

you can post a question at customer support to rectify the situation
0
 

Author Comment

by:wsteph
ID: 2775816
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
 
LVL 3

Expert Comment

by:darinw
ID: 2779043
Hello everyone,

AzraSound: Please see your question:

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

darinw
Customer Service
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now