Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SHELL - Hold Execution 1

Status
Not open for further replies.

leummens

Technical User
Jul 29, 2002
13
NL
I'm opening a external program using the SHELL command.

However, execution of the script should hold until this external program has closed.

The shell function returns an ID, but I don't know how I can use this

Any suggestions on how I could hold execution?

Mark
 
Put this in a module
Code:
Option Explicit
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject 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 WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = -1&

Public Sub RunUntilFinished(ByVal sApp As String)
    Dim lProcID As Long
    Dim hProc As Long

    ' Start the App
    On Error GoTo ErrHndlr
    lProcID = Shell(sApp, vbNormalFocus)
    On Error GoTo 0

    DoEvents

    ' Wait for the App
    hProc = OpenProcess(SYNCHRONIZE, 0, lProcID)
    If hProc <> 0 Then
        WaitForSingleObject hProc, INFINITE
        CloseHandle hProc
    End If
    Exit Sub

ErrHndlr:
    MsgBox "Error starting App:" & vbCrLf & _
           "App: " & sApp & vbCrLf & _
           "Err Desc: " & err.Description
    err.Clear
End Sub

Then call it like this:
Code:
sPRGM = "C:\Program Files\Microsoft Office\Office\MSACCESS.EXE ""Q:\dir\mydb.mdb"" /x updatedaily"
RunUntilFinished sPRGM
HTH
 
Perfect, just what I was lookig for.

Thanx,
Mark
 
Glad I could help. I've gotten more help here than I could probably every give! :)
 
A simpler one line way:
CreateObject("WScript.Shell").Run """\path\to\program.exe"" ""\path\to\somefile.ext"" OtherArg", 3, True

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top