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
------------------------------------------------------------------
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
------------------------------------------------------------------