open and close another app from macro

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

Joel

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
 
J

John Keith

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

Joel

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

John Keith

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)
 

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