ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Shell can open an application, can it close an app too? (https://www.excelbanter.com/excel-programming/328185-shell-can-open-application-can-close-app-too.html)

quartz[_2_]

Shell can open an application, can it close an app too?
 
I am using Office 2003 on Windows XP.

I use Shell to open foreign apps like Acrobat and MODI, but can Shell (or
some other command) be used to close these applications as well? If so, how?

Could someone please post example code? Thanks much in advance.

K Dales[_2_]

Shell can open an application, can it close an app too?
 
No, Shell cannot close the app. You will need to do it another way. The
simplest way is probably to use SendKeys to send the proper command to close
the app; most often this would be Alt-F to get the file menu and then X to
Exit. Or Alt-F4 often closes the app. But SendKeys can be tricky since you
need to make sure you know exactly the state the other application is in to
know that the key combination will do what you want it to do, and also in
rare occasions I have had other apps grab focus before SendKeys can act (e.g.
I get an email notification from Groupwise between the AppActivate and
SendKeys commands), which then sends the keys to the wrong application with
potentially disasterous results. In other words, use the following code with
discretion and test it under a variety of conditions.

The code would be:

AppActivate "AppTitleBarText"
' AppTitleBarText is the exact text in the title bar of the application you
want activated
' Note: You can use the TaskID returned by Shell instead of the title bar text
SendKeys "%FX", True
' Sends the keys specified to the activae application - this is AltF and X

"quartz" wrote:

I am using Office 2003 on Windows XP.

I use Shell to open foreign apps like Acrobat and MODI, but can Shell (or
some other command) be used to close these applications as well? If so, how?

Could someone please post example code? Thanks much in advance.


quartz[_2_]

Shell can open an application, can it close an app too?
 
Yeah, I was hoping for something better than SendKeys as I am well aware of
its potential for disaster.

Thanks K. for your post.

"K Dales" wrote:

No, Shell cannot close the app. You will need to do it another way. The
simplest way is probably to use SendKeys to send the proper command to close
the app; most often this would be Alt-F to get the file menu and then X to
Exit. Or Alt-F4 often closes the app. But SendKeys can be tricky since you
need to make sure you know exactly the state the other application is in to
know that the key combination will do what you want it to do, and also in
rare occasions I have had other apps grab focus before SendKeys can act (e.g.
I get an email notification from Groupwise between the AppActivate and
SendKeys commands), which then sends the keys to the wrong application with
potentially disasterous results. In other words, use the following code with
discretion and test it under a variety of conditions.

The code would be:

AppActivate "AppTitleBarText"
' AppTitleBarText is the exact text in the title bar of the application you
want activated
' Note: You can use the TaskID returned by Shell instead of the title bar text
SendKeys "%FX", True
' Sends the keys specified to the activae application - this is AltF and X

"quartz" wrote:

I am using Office 2003 on Windows XP.

I use Shell to open foreign apps like Acrobat and MODI, but can Shell (or
some other command) be used to close these applications as well? If so, how?

Could someone please post example code? Thanks much in advance.


Jim Rech

Shell can open an application, can it close an app too?
 
Shell cannot close an app, nor is there any way built into VB to do that, as
far as I know.

This is code I found doing a Google search. Seems to work as an example.

Private Declare Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const PROCESS_TERMINATE = 1


Sub OpenClose()
Dim ProcessHandle As Long, PID As Long
PID = CLng(Shell("notepad.exe", vbNormalFocus))
Call Sleep(5000)
ProcessHandle = OpenProcess(PROCESS_TERMINATE, 0, PID)
Call TerminateProcess(ProcessHandle, 0)
Call CloseHandle(ProcessHandle) 'Clean up after ourselves
End Sub

--
Jim
"quartz" wrote in message
...
|I am using Office 2003 on Windows XP.
|
| I use Shell to open foreign apps like Acrobat and MODI, but can Shell (or
| some other command) be used to close these applications as well? If so,
how?
|
| Could someone please post example code? Thanks much in advance.



K Dales[_2_]

Shell can open an application, can it close an app too?
 
The better option is to use the Windows API functions, but that takes some
advanced coding; if interested look at
http://msdn.microsoft.com/library/de...y_category.asp

"quartz" wrote:

Yeah, I was hoping for something better than SendKeys as I am well aware of
its potential for disaster.

Thanks K. for your post.

"K Dales" wrote:

No, Shell cannot close the app. You will need to do it another way. The
simplest way is probably to use SendKeys to send the proper command to close
the app; most often this would be Alt-F to get the file menu and then X to
Exit. Or Alt-F4 often closes the app. But SendKeys can be tricky since you
need to make sure you know exactly the state the other application is in to
know that the key combination will do what you want it to do, and also in
rare occasions I have had other apps grab focus before SendKeys can act (e.g.
I get an email notification from Groupwise between the AppActivate and
SendKeys commands), which then sends the keys to the wrong application with
potentially disasterous results. In other words, use the following code with
discretion and test it under a variety of conditions.

The code would be:

AppActivate "AppTitleBarText"
' AppTitleBarText is the exact text in the title bar of the application you
want activated
' Note: You can use the TaskID returned by Shell instead of the title bar text
SendKeys "%FX", True
' Sends the keys specified to the activae application - this is AltF and X

"quartz" wrote:

I am using Office 2003 on Windows XP.

I use Shell to open foreign apps like Acrobat and MODI, but can Shell (or
some other command) be used to close these applications as well? If so, how?

Could someone please post example code? Thanks much in advance.



All times are GMT +1. The time now is 10:19 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com