Terminate MS Excel Process from VBA

E

Edward Nelsas

I am currently working with Reflections, a terminal emulator software which
includes Microsoft Visual Basic for Applications Version 6.3 for recording /
writing scripts. My current project involves a data driven script which
accesses an Excel document for outputting retrieved data to the terminal
screen and writing results to the Excel file. My script simply opens the
excel file, reads/writes data and then closes the document and destroys all
Excel objects.

The problem I have encountered is when a problem occurs with the script and
the Excel file is never closed, hence an Excel process remains running in
the background which can be seen in the Task Manager. When I try to re-run
the script with the old Excel process still running, a prompt is displayed
to save the file.

My goal for this script is to check to see if any Excel processes are
running and terminate them before opening the Excel file.

My current solution to this problem is to find the Excel (XLMAIN) windows
handle, determine the proess ID from the windows handle and then kill the
process, but I still am having no luck...

I have attached the code that I am currently using. The code appears to be
finding the windows handle and process ID, but it does not seem to kill the
process and it remains in the task manager.

I would greatly appreciate any suggestions or other possible solutions to
this problem....


Thank You

Edward Nelsas

--------------------------------------------------------------------------
Const conXLSClsName As String = "XLMAIN"
Const PROCESS_ALL_ACCESS = &H1F0FFF
Dim lngXLSHandle As Long
Dim lngThreadId As Long

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

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

Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hWnd As Long, lpdwProcessId 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 Sub sKillProcess(ByVal hWnd As Long)

'--- Kills a running application

'--- Parameter
' hWnd: the application's window handle

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

lngRtn = GetWindowThreadProcessId(hWnd, lngProcID)
lngProc = OpenProcess(PROCESS_ALL_ACCESS, CLng(0), lngProcID)
lngRtn = TerminateProcess(lngProc, CLng(0))

End Sub

' Routine called to kill process
Sub EndExcelProcess()

lngXLSHandle = FindWindow(conXLSClsName, 0)
lngThreadId = GetWindowThreadProcessId(lngXLSHandle, 0)
Call sKillProcess(lngThreadId)

End Sub
------------------------------------------------------------------
 
C

Chip Pearson

Edward,

Are you calling Application.Quit to quit the Excel application?


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
T

Tushar Mehta

A more elegant solution than trying to find and terminate the XL
process(es) might be to ensure it goes away in the first place.

An undocumented 'connection' is created when the initiating process
uses an XL global property/method without qualification. This 'behind
the scene' connection between the initiator and XL is closed only when
the initiator goes away.

In addition to Chip's point about ensuring there is a XL.Quit statement
in your code, you must ensure that *every* reference to an XL
object/property/method is through the XL object variable you have.

For more, see the discussion that includes
http://groups.google.com/groups?selm=MPG.1a209c37c53c871198ad4a%
40msnews.microsoft.com

--
Regards,

Tushar Mehta, MS MVP -- Excel
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions
 

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

Top