Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2007

Tip: Looking for answers? Try searching our database.

Run external program

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ed - 17 Jul 2007 10:01 GMT
I know that I can use Shell to run an external program, but can I check to
see if the "Shelled" program is already running so I don't end up with 2 (or
5) of the same program running at the same time?

Ed (in Virginia)
Russ - 17 Jul 2007 10:20 GMT
Use one of these methods to wait for shell process end.

You could even call a dos batch file from VBA.
Helmut Weber mentioned this:
<http://vb.mvps.org/samples/project.asp?id=Shell32>

Or this xShell code works in Word97, too:

Put this in Declarations section at the top of your VBA code module so that
all subroutines can take advantage of the 'wait for shell' code.

Private Declare Function CloseHandle Lib "kernel32" ( _
   ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" ( _
   ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" ( _
   ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
   ByVal dwProcessId As Long) As Long

'Add this code as one of the regular subroutines.

Public Function ShellX( _
   ByVal PathName As String, _
   Optional ByVal WindowStyle As Integer = vbMinimizedFocus, _
   Optional ByVal Events As Boolean = True _
 ) As Long

 'Declarations:
 Const STILL_ACTIVE = &H103&
 Const PROCESS_QUERY_INFORMATION = &H400&
 Dim ProcId As Long
 Dim ProcHnd As Long

 'Get process-handle:
 ProcId = Shell(PathName, WindowStyle)
 ProcHnd = OpenProcess(PROCESS_QUERY_INFORMATION, True, ProcId)

 'wait for process end:
 Do
   If Events Then DoEvents
   GetExitCodeProcess ProcHnd, ShellX
 Loop While ShellX = STILL_ACTIVE

 'clean up:
 CloseHandle ProcHnd
End Function

'And call it like this:

  Dim x As Long
  Dim strDosBatchFullPath As String
  strDosBatchFullPath = ³C:\...myDosBatchFile.bat²
  System.Cursor = wdCursorWait
  x = ShellX(Chr(34) & strDosBatchFullPath & Chr(34))

> I know that I can use Shell to run an external program, but can I check to
> see if the "Shelled" program is already running so I don't end up with 2 (or
> 5) of the same program running at the same time?
>
> Ed (in Virginia)

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Steve Yandl - 17 Jul 2007 14:25 GMT
The Word application object has a 'Tasks' property that will return the
friendly names of running processes.  This will let you check for a running
instance of the program whether it was started using Shell or started some
other way (like the user launched it before your subroutine was started).

Steve

>I know that I can use Shell to run an external program, but can I check to
>see if the "Shelled" program is already running so I don't end up with 2
>(or 5) of the same program running at the same time?
>
> Ed (in Virginia)
Ed - 18 Jul 2007 10:33 GMT
Thanks, Steve and Russ,

   I did end up using the 'Tasks' feature. I had several different programs
running with the same "friendly names" so I had to change the names of the
one I was trying to manipulate to make sure that I was selecting the right
one.

   What I really hoped to do was to change a value of the running program
(one that I had set in the first instance using a command line parameter),
but found that such was not possible. So I used Task to determine if the
program was open, and if it was, I closed it with the ".Close" method, and
just reopened (with Shell) the program with the new command line parameters.

  Works like a charm. Thank for the guidance.

    -Ed (in Virginia)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.