Results 1 to 8 of 8
  1. #1
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Bat file errorlevel in VBA (Win98 / Word97 (NL))

    Is there a way in VBA to get the ErrorLevel from a Dos bat file? I searched the Lounge for "errorlevel" but nothing uselfull came up.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Bat file errorlevel in VBA (Win98 / Word97 (NL))

    I don't think there is a direct way to retrieve the error level, but others may prove me wrong.

    Can you modify the batch file? If so, you could create a text file in the batch file with info about the error (using ECHO ... > filename.txt), and open the text file in VBA.

  3. #3
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Quedgeley, Gloucester, England
    Posts
    5,333
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Bat file errorlevel in VBA (Win98 / Word97 (NL

    I may be nit-picking (for I have been accused of that), but a DOS BATch file does not and cannot generate an errorlevel, of itself.

    On leaving a BATch file the outstanding errorlevel is that which has been set by the last program to execute before the BATch file terminates. A program may not bother to set an errorlevel, therefore it will show as zero. This errorlevel value is held withing the running copy of COMMAND.COM, or now more likely CMD.EXE, so if that terminates then it would not be possible for VBA to access it.

    Unfortunately I have never used VBA so I cannot say anything further...

    John
    <font face="Script MT Bold"><font color=blue><big><big>John</big></big></font color=blue></font face=script>

    Ita, esto, quidcumque...

  4. #4
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Bat file errorlevel in VBA (Win98 / Word97 (NL

    If you SET an environment string within the BAT file based on a returned errorlevel, you can use it in VB.
    -John ... I float in liquid gardens
    UTC -7ąDS

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Bat file errorlevel in VBA (Win98 / Word97 (NL))

    The following is a hacked modification of the example in Help for the Environ function.

    You could try tweaking it to see if you can ge tthe value of ErrorLevel.
    <pre>Option Explicit

    Private Sub GetEnvironVariable()
    Const strTarget As String = "Path"
    Dim EnvString As String
    Dim Indx As Long
    Dim Msg As String
    Dim PathLen As Long
    Indx = 1
    Do
    EnvString = Environ(Indx)
    If Left(EnvString, Len(strTarget) + 1) = strTarget & "=" Then
    PathLen = Len(Environ(strTarget))
    Msg = strTarget & " entry = " & Indx & " and length = " & PathLen
    Exit Do
    Else
    Indx = Indx + 1
    End If
    Loop Until EnvString = ""
    If PathLen > 0 Then
    MsgBox Msg
    Else
    MsgBox "No " & strTarget & " environment variable exists."
    End If
    End Sub
    </pre>


  6. #6
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Bat file errorlevel in VBA (Win98 / Word97 (NL))

    You guys educated me once again! A big <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15> to all of you.

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Bat file errorlevel in VBA (Win98 / Word97 (NL

    Just curious:

    Why do you think the example inflicts a loop when a simple <pre>Environ("Path")</pre>

    would have worked as well?
    (In my Win98SE the environment variables seem to be forced into upper-case)

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Bat file errorlevel in VBA (Win98 / Word97 (NL

    I just hacked the example in the Help file, I did not fix the code.
    The example is very poor code, a not uncommon thing in the Help files.

    The comparison, in proper code, does have to do a case insensitive compare.

Posting Permissions

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