Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Old April 26th 05, 01:41 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Jul 2004
Posts: 441
Default 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.

  #2   Report Post  
Old April 26th 05, 02:50 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Aug 2004
Posts: 1,163
Default 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.

  #3   Report Post  
Old April 26th 05, 03:02 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Jul 2004
Posts: 441
Default 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.

  #4   Report Post  
Old April 26th 05, 03:11 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Jul 2006
Posts: 2,718
Default 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.


  #5   Report Post  
Old April 26th 05, 03:30 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Aug 2004
Posts: 1,163
Default 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.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Close the other application Lulu Excel Programming 1 December 16th 04 06:43 PM
Shell to different app then open file within Justin[_12_] Excel Programming 0 December 1st 04 12:26 AM
macro to close excel application other than application.quit mary Excel Programming 1 September 14th 04 03:43 PM
Application.Close Nichevo Excel Programming 3 December 23rd 03 04:33 PM
Application Process-Shell Command GarethG[_10_] Excel Programming 2 October 29th 03 02:35 PM


All times are GMT +1. The time now is 09:49 PM.

Powered by vBulletin® Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.
Copyright 2004-2021 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"

 

Copyright © 2017