On Error

Syntax

On Error Goto Label

On Error Resume Next

On Error Goto 0

Remarks

Establishes an "error handler" — typically, a series of instructions that takes over when an error occurs. When an error occurs in a macro that does not contain the On Error statement, an error message is displayed and the macro quits.

This form

Performs this action

On Error Goto Label

Jumps from the line where the error occurred to the specified label. The instructions following this label can then determine the nature of the error (using the special variable Err) and take some appropriate action to correct or resolve the problem. For more information, see Err.

On Error Resume Next

Continues running the macro from the line that follows the line where the error occurred and resets Err to 0 (zero). In effect, the error is ignored.

On Error Goto 0

Disables the error trapping established by an earlier On Error Goto or On Error Resume Next statement and sets Err to 0 (zero).


Once an error triggers an error handler, no further error handling occurs until Err is reset to 0 (zero). Usually, you should place an Err = 0 instruction at the end of your error handler. Do not include Err = 0 in the middle of an error handler or you risk creating an endless loop if an error occurs within the handler.

Note that an error handler established in the main subroutine is not in effect when control passes to another subroutine. To trap all errors, each subroutine must have its own On Error statement and error handler. After control is returned to the main subroutine, the main On Error instruction is again in effect.

WordBasic generates errors with numbers less than 1000; Word itself generates errors with numbers 1000 or greater. Error handlers can trap both WordBasic and Word errors. However, if a Word error occurs, an error message is displayed, and the user must respond before the macro can continue. When the user chooses the OK button, control passes to the error handler.

For a complete list of all WordBasic and Word error messages and error numbers, see "Error Messages" later in this part.

Examples

This example shows a common use of On Error Resume Next to avoid WordBasic error number 102, "Command failed," when a user cancels a dialog box or prompt:


On Error Resume Next
A$ = InputBox$("Your name please:")

The following macro prompts the user to specify a sequential file for input (for example, a text-only file containing a list of Word documents). If the file cannot be found, the instructions following the label specified by On Error Goto Label suggest a reason corresponding to the error number.


Sub MAIN
On Error Goto ErrorHandler
DocName$ = InputBox$("Filename for input:", "", DocName$)
Open DocName$ For Input As #1
'Statements that use the input go here
Close #1
Goto Done                'If there is no error, skip the error handler
ErrorHandler:
Select Case Err
    Case 53 : MsgBox "The file " + DocName$ + " does not exist."
    Case 64 : MsgBox "The specified drive is not available."
    Case 76 : MsgBox "The specified folder does not exist."
    Case 102                'If the user cancels the dialog box
    Case Else : MsgBox "Error" + Str$(Err) + " occurred."
End Select
Err = 0
Done:
End Sub

See Also

Err, Error, Goto, Select Case