Question : VBA - Controlling the Shell function

I use VBA code in an Excel workbook to pull in data from unix boxes, which is then reformated/recalculated and emailed to various people.

To pull the data in I use the shell function to run a Perl script, which basically runs an ftp.

My problem is that because the shell function runs asynchronously I need some way of telling the Perl script has completed before allowing my code to continue.

I have been using a work around using a msgbox to halt the code, but I'm sure there must be a better way.

He's an excerpt from my code as an example of what I'm doing.

Sub

day_num = Sheets("WORK").Range("B1")

Dim RetVal, Style, Response
RetVal = Shell("J:\autorec\perl.exe J:\autorec\myperlscript.pl", 1)
Msg = "Was File Transfer Successful ?"
    Style = vbYesNo + vbQuestion
    Response = MsgBox(Msg, Style)
    If Response = vbNo Then
        Sheets("DAY" & day_num).Select
        Exit Sub
    End If

etc...
End Sub

What I would like is the code to run the shell script and then wait until it completes before carrying on, without having the user to intervene in some way.

Answer : VBA - Controlling the Shell function

toffee,

I'm too lazy to read through all the previously posted code. Here is an alternate approach that may apply to your problem...

If your FTP is returning a file with a known file name and file location, you could use this little loop to check if it has finished downloading.

Sub LoopUntilFileIsAvailable()
  Dim f As Integer
  f = FreeFile
  On Error Resume Next
  Do
    Open "c:\test\test.txt" For Input Lock Read As f
  Loop Until FreeFile <> f
  On Error GoTo 0
  Close f
End Sub

Ture Magnusson
Karlstad, Sweden
Random Solutions  
 
programming4us programming4us