Results 1 to 5 of 5
  1. #1
    Lounger
    Join Date
    Jan 2001
    Location
    Bay Area, California, USA
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exit Codes from external program (Any VBA 6 client or VB 6)

    Winzip has a command line utility, WZZIP.exe and WZUNZIP.exe (http://www.winzip.com/wzcline.htm) which I can run from my VBA client to zip/unzip files w/o the Winzip UI.

    There are many way to run an external program and determine whether the call succeeded or failed. What I don't know is how to retrieve the return code (exit code) from the called program itself.

    WZZIP.exe returns an errorlevel of 0 if it succeeded, or nonzero if there was an error. Again, I know if WZZIP was successfully launched, but I don't know how to determine if WZZIP encountered any errors, since I don't know how to capture it's exit codes.

    In a batch file, I could use IF ErrorLevel nn (which returns true if the previous program executed by command.com had an exit code equal to or greater than nn), e.g.

    wzzip filename.zip *.doc
    If Not ErrorLevel 1 Goto Exit
    Echo ***SOME ERROR ***
    :Exit

    This should work with any shelled program, as long as it returns an exit code. I'm sure this is done "all the time".

    Any ideas?

  2. #2
    Bronze Lounger
    Join Date
    Jun 2001
    Location
    New York, New York, Lebanon
    Posts
    1,449
    Thanks
    1
    Thanked 1 Time in 1 Post

    Re: Exit Codes from external program (Any VBA 6 client or VB 6)

    <img src=/S/hello.gif border=0 alt=hello width=25 height=29> Klaus

    My two cents here:

    I had to automate an application that would get a zipped MS-Excel workbook and unzip it and then use the unzipped components. I thought it was easy but then the horror stories started showing up.

    Basically the <font color=blue> SHELL </font color=blue> command can return an exit code in the OLH of Excel 97 it says:

    <font color=red> Runs an executable program and returns a Variant (Double) representing the program's task ID if successful, otherwise it returns zero. </font color=red> so what I have been using is something like that:

    <font color=blue> vWinZipApp = Shell(sPathToWinZip & "WINZIP32.EXE -e -o " & sShortPathToFile & " " _
    & sPathToFile, vbMinimizedFocus) </font color=blue>


    But to really test the success of the unzipping process, I thought of counting how many workbooks do I have in the folder first and then do the unzipping and then pause the code until the SHELL command has finished by using a loop that recounts the workbooks in that folder, once they increase I know some unzipping has taken place.

    Then I discovered an API, I'll send it to you later, and that did a better job in detecting when the process ended and that was it.

    WinZip is not programmer friendly and that is a shame, I hope someone from WinZip notices and gets us some control over this process.

    HTH.

    Wassim <img src=/S/compute.gif border=0 alt=compute width=40 height=20>
    <img src=/S/compute.gif border=0 alt=compute width=40 height=20> in the <img src=/S/bagged.gif border=0 alt=bagged width=22 height=22>

  3. #3
    Lounger
    Join Date
    Jan 2001
    Location
    Bay Area, California, USA
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Exit Codes from external program (Any VBA 6 client or VB 6)

    Wassim,

    thanks for replying.

    Correct, Shell returns the shelled program's Task ID, but this basically only tells you if the shelled program was =launched= correctly. If not, it returns 0.

    What I need is to capture the return code generated by the shelled program itself =after= it was launched.

    Step 1: "Shell" launches program correctly, returns a non-zero task id and is done.

    Stpe 2: Launched program attempts to perform the requested operation (i.e., add files to a zip archive), and returns whether it was successful, or if there was trouble (e.g., the file to be zipped does not exist, disk full etc). That's the value I need to capture.

    Re.: "WinZip is not programmer friendly " - you may want to try their new WinZip Command Line download. It only has two commands, WZZIP and WZUNZIP, with all the commandline parameters you need. No UI to get in the way.

    Klaus

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Exit Codes from external program (Any VBA 6 client or VB 6)

    I didn't download the programs to test, but in theory something like this should work:

    <pre>Function EnvVar(strCommandLine As String) As String
    ' Set a reference to Windows Script Host Object Model
    ' Instantiate Shell object and Environment collection
    Dim wshShell As New IWshShell_Class
    Dim wshEnv As IWshEnvironment_Class
    Set wshEnv = wshShell.Environment("Process")
    ' Run program in shell
    Dim lngShellReturn As Long
    lngShellReturn = wshShell.Run(strCommandLine, WshMinimizedNoFocus, True)
    ' Return errorlevel, if any, as a string
    EnvVar = wshEnv("errorlevel")
    End Function

    Sub TestEnvVar()
    MsgBox EnvVar("c:test.bat")
    End Sub</pre>


  5. #5
    Lounger
    Join Date
    Jan 2001
    Location
    Bay Area, California, USA
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Exit Codes from external program (Any VBA 6 client or VB 6)

    Thanks for the help.

    Although strCommandLine executes without a problem, EnvVar doesn't return anything. All I get is a blank MsgBox when I try with WZZIP and Notepad.

    I was thinking there might be a solution in WSH somewhere, I just didn't know how to get started.

    Klaus

Posting Permissions

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