open and close another app from macro

  • Thread starter Thread starter John Keith
  • Start date Start date
J

John Keith

I have used the following line of code to open another application
from an excel macro:

AppVal = Shell("C:\Program Files\SAP\saplogon.exe")

Is there something similar I can execute to close this application?




John Keith
(e-mail address removed)
 
the shell function returns a handle. the TerminateProcess uses a handle to
kill the process. You just have to declare the definiation of the
TerminateProcess function outside a routine like th code below.



Private Declare Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, _
ByVal uExitCode As Long) As Long

Sub Terminate()

AppVal = Shell("C:\Program Files\SAP\saplogon.exe")

lRetValue = TerminateProcess(AppVal, 0&)

End Sub
 
the shell function returns a handle. the TerminateProcess uses a handle to
kill the process. You just have to declare the definiation of the
TerminateProcess function outside a routine like th code below.



Private Declare Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, _
ByVal uExitCode As Long) As Long

Sub Terminate()

AppVal = Shell("C:\Program Files\SAP\saplogon.exe")

lRetValue = TerminateProcess(AppVal, 0&)

End Sub

Thank you Joel, I'll do some more study as this opens some new
territory for me.
John Keith
(e-mail address removed)
 
The handle of a window is the Process ID. Every window has a handle which in
VBA is HWND. To get a window you can use the parent of the window like this

handle = thisworkbook.parent.hwnd

You can also activate the window using either the title of the Window or the
process number

AppActivate title (or handle)

like this

AppActivate "Book1.xls"

Then you can get the handle from the activewindow

AppActivate "Book1"
Handle = ActiveWindow.Application.Hwnd


You can also use any Win32 dll to get the process number.
 
The handle of a window is the Process ID. Every window has a handle which in
VBA is HWND. To get a window you can use the parent of the window like this

handle = thisworkbook.parent.hwnd

Joel,

Thank you for even further detail! (I get so frustrated trying to find
good VBA documentation!)


John Keith
(e-mail address removed)
 
Back
Top