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

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Bat file errorlevel in VBA (Win98 / Word97 (NL))

    Author
    Topic
    #412103

    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.

    Viewing 5 reply threads
    Author
    Replies
    • #898407

      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.

    • #898408

      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.

    • #898447

      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

      • #898569

        If you SET an environment string within the BAT file based on a returned errorlevel, you can use it in VB.

      • #898570

        If you SET an environment string within the BAT file based on a returned errorlevel, you can use it in VB.

    • #898448

      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

    • #898877

      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.

      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
      
      • #898924

        You guys educated me once again! A big thankyou to all of you.

      • #899206

        Just curious:

        Why do you think the example inflicts a loop when a simple

        Environ("Path")

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

        • #899217

          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.

        • #899218

          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.

      • #899207

        Just curious:

        Why do you think the example inflicts a loop when a simple

        Environ("Path")

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

    • #898878

      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.

      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
      
    Viewing 5 reply threads
    Reply To: Reply #898407 in Bat file errorlevel in VBA (Win98 / Word97 (NL))

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel