Results 1 to 3 of 3

Thread: Wait for shell script to execute?[RESOLVED]

  1. #1

    Thread Starter
    Addicted Member perlmonk's Avatar
    Join Date
    Jan 2005
    Posts
    138

    Resolved 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!!!
    Last edited by perlmonk; Jan 14th, 2005 at 11:12 AM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Wait for shell script to execute?

    Search the Forums for the WaitForSingleObject API.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Addicted Member perlmonk's Avatar
    Join Date
    Jan 2005
    Posts
    138

    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:
    1. Function RunPerl(sPerlScriptToRun As String, _
    2.                    sSemFolderName As String, _
    3.                    sngWaitMax As Single) As Boolean
    4.  
    5.   Dim sPerlPath As String
    6.   Dim sFullShellCommand As String
    7.   Dim sSemDir As String
    8.   Dim sSemDirFullName As String
    9.   Dim sngStartTime As Single
    10.  
    11.   ' Full path of "Windowless" Perl executable
    12.   sPerlPath = "C:\perl\bin\wperl.exe"
    13.  
    14.   ' Get the full path from the environment variable
    15.   sSemDirFullName = Environ("TEMP") & "\" & sSemFolderName
    16.  
    17.   ' Put quotes around full script path.
    18.   ' This allows for spaces in script path names, common on Windows systems.
    19.   sFullShellCommand = sPerlPath & " " & _
    20.     Chr(34) & sPerlScriptToRun & Chr(34)
    21.  
    22.   ' Create semaphore directory, unless it already exists
    23.   If Not LCase(Dir(sSemDirFullName, vbDirectory)) = LCase(sSemFolderName) Then
    24.     MkDir (sSemDirFullName)
    25.   End If
    26.  
    27.   ' Start the countdown to timeout
    28.   sngStartTime = Timer
    29.  
    30.   ' Run Perl script
    31.   Shell (sFullShellCommand)
    32.  
    33.   ' The script will stay in this loop until either
    34.   ' the semaphore directory is deleted, or until the
    35.   ' time limit set by sngMaxWaitTime has passed
    36.   Do While LCase$(Dir$(sSemDirFullName, vbDirectory)) = _
    37.                   sSemFolderName And _
    38.      ((Timer - sngStartTime) < sngWaitMax)
    39.     ' Display a countdown in status bar
    40.     StatusBar = "Waiting " & _
    41.       Int((sngWaitMax - (Timer - sngStartTime))) & _
    42.       " more seconds for Perl ..."
    43.  
    44.   Loop
    45.  
    46.   If LCase$(Dir$(sSemDirFullName, vbDirectory)) = sSemFolderName Then
    47.     ' Gave up waiting.
    48.     RmDir (sSemDirFullName)
    49.     StatusBar = "Gave up waiting for Perl"
    50.     RunPerl = False
    51.   Else
    52.     ' Perl script successfully deleted semaphore folder
    53.     StatusBar = ""
    54.     RunPerl = True
    55.   End If
    56.  
    57. End Function

    Thanks once again...your help has been invaluable.

Posting Permissions

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



Click Here to Expand Forum to Full Width