Microsoft Office 2000/Visual Basic Programmer's Guide   

Handling VBScript Run-Time Errors

Although VBScript provides the Err object and that object exposes the same methods and properties available in the VBA Err object, writing error handlers using VBScript is not the same as in VBA. The primary limitation is due to the limited functionality of the On Error statement in VBScript. In VBScript you can't branch to an error handler by using the familiar On Error GoTo ErrorHandler syntax. You can only enable error handling in VBScript by using the On Error Resume Next syntax.

The following code excerpt shows the error-handler portion of the script in ScriptErrors.htm in the ODETools\V9\Samples\OPG\Samples\CH08 subfolder on the Office 2000 Developer CD-ROM. The script performs simple division and then immediately checks to see if an error occurred and responds accordingly:

intResult = intNumerator/intDenominator

' Check for errors as a result of the division.
If Err <> 0 Then
   Select Case Err.Number
      Case DIVIDE_BY_ZERO
         If Len(txtDenominator.Value) = 0 Then
            strErrorResultText = "Missing!"
         Else
            strErrorResultText = "'" & txtDenominator.Value & "'"
         End If
         strErrorMessage = "Error: " & Err.Number & _
            vbCrLf & vbCrLf & "The value you entered in the " _
            & "text box was: " & strErrorResultText
         txtDenominator.Focus
      Case Else
         strErrorMessage = "Error: " & Err.Number & _
            vbCrLf & vbCrLf & "Unrecognized error!"
   End Select
   MsgBox strErrorMessage, CRITICAL_ERROR + MSGBOX_OKONLY, _
      "Error Type = " & Err.Description 
End If