Run external program

E

Ed

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

Russ

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

Steve Yandl

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
 
E

Ed

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)
 

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