Results 1 to 3 of 3

Thread: Shell Command

  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I use a SHELL command in a line of VBA and it works just fine. However the code proceeds to the next line without waiting for the SHELL command to finish.

    Is there a line of code that I can use to wait for the SHELL command to finish prior to processing the next line of code?

    Thanks for your assistance,
    John

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    See for example Shell And Wait.

  3. #3
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is an alternative solution to what Chip Pearson posts; tests an infinite loop on the processID and waits until it finishes prior to running the next line of code. I took bits and pieces from Chip Pearson and others to reduce the amount of lines to what you see below.

    [codebox]Public Declare Function OpenProcess Lib "kernel32" _
    (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long

    Public Declare Function WaitForSingleObject Lib "kernel32" _
    (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Public Const SYNCHRONIZE = &H100000
    Public Const INFINITE = &HFFFF


    Sub Start()
    Dim lngRtn As Long
    Dim lngProID As Long
    Dim lngProHn As Long


    'Process 1st SHELL call
    lngProID = Shell("Enter your shell statement here")
    GoSub Continue_Process_Check:

    'Process 2nd SHELL call
    lngProID = Shell("Enter your shell statement here")
    GoSub Continue_Process_Check:

    MsgBox "Finished"
    Exit Sub
    Continue_Process_Check:
    lngProHn = OpenProcess(SYNCHRONIZE, True, lngProID)
    lngRtn = WaitForSingleObject(lngProHn, INFINITE)
    lngRtn = CloseHandle(lngProHn)
    Return
    End Sub
    [/codebox]


    Regards,
    John

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •