Wait for shell script to execute?[RESOLVED]
Hey guys..first of all...thanks for all the previous help i got on my VB code..everything is working like a charm... I have another minor issue, and after this i shud be done (hopefully).
My macro creates two shell scripts which I want to execute from within the macro itself. I know I can execute the scripts by using the Shell() function, but how do I make my macro wait for the script to finish before proceeding? This is because the first script has to be executed to generate the input file for the sceond script. I hope this is a common problem and easy to resolve....please lemme know...thanks!!!
Re: Wait for shell script to execute?
Search the Forums for the WaitForSingleObject API.
Re: Wait for shell script to execute?
Hey...thanks for that suggestion...I found a lot of threads pertaining to what i was doing. Before your post, I had actually started looking thru some websites myself, and I Started working thru some code.
I found a sub on one website, that required some level of customization, but now, it works like a charm...here is and excerpt and code from that website:
Since it's likely you'd want to access a variety of Perl scripts from within a Word macro, it's worthwhile to create a reusable function to act as a wrapper around the Shell function call to Perl. The following function takes three arguments: the name of the Perl script to run, the name of the semaphore folder the Perl script should delete when it finishes, and finally the maximum time to wait for the Perl script to run before giving up. The function returns a value of True if the Perl script deleted the semaphore folder, or False if the folder still exists when the time limit is reached. Put this code into the template of your choice [Hack #50]:
VB Code:
Function RunPerl(sPerlScriptToRun As String, _
sSemFolderName As String, _
sngWaitMax As Single) As Boolean
Dim sPerlPath As String
Dim sFullShellCommand As String
Dim sSemDir As String
Dim sSemDirFullName As String
Dim sngStartTime As Single
' Full path of "Windowless" Perl executable
sPerlPath = "C:\perl\bin\wperl.exe"
' Get the full path from the environment variable
sSemDirFullName = Environ("TEMP") & "\" & sSemFolderName
' Put quotes around full script path.
' This allows for spaces in script path names, common on Windows systems.
sFullShellCommand = sPerlPath & " " & _
Chr(34) & sPerlScriptToRun & Chr(34)
' Create semaphore directory, unless it already exists
If Not LCase(Dir(sSemDirFullName, vbDirectory)) = LCase(sSemFolderName) Then
MkDir (sSemDirFullName)
End If
' Start the countdown to timeout
sngStartTime = Timer
' Run Perl script
Shell (sFullShellCommand)
' The script will stay in this loop until either
' the semaphore directory is deleted, or until the
' time limit set by sngMaxWaitTime has passed
Do While LCase$(Dir$(sSemDirFullName, vbDirectory)) = _
sSemFolderName And _
((Timer - sngStartTime) < sngWaitMax)
' Display a countdown in status bar
StatusBar = "Waiting " & _
Int((sngWaitMax - (Timer - sngStartTime))) & _
" more seconds for Perl ..."
Loop
If LCase$(Dir$(sSemDirFullName, vbDirectory)) = sSemFolderName Then
' Gave up waiting.
RmDir (sSemDirFullName)
StatusBar = "Gave up waiting for Perl"
RunPerl = False
Else
' Perl script successfully deleted semaphore folder
StatusBar = ""
RunPerl = True
End If
End Function
Thanks once again...your help has been invaluable.