Err
Syntax
Err = 0
Error Err
ErrorNum = Err
Remarks
Err is a special variable whose value is the error code for the most recent error condition.
- Err = 0 is ued to reset error trapping after an error has occurred, normally at the end of an error trap.
- Error Err displays the message associated with the most recent error condition and stops the macro.
- ErrorNum = Err assigns the value of Err to the variable ErrorNum.
For more information on trapping errors, see On Error.
Example
This macro prompts the user to type a style name that is then applied to the selected paragraphs. The macro contains an error handler that illustrates the use
of Err within a Select Case control structure. The Select Case control structure
in this macro can be described as follows:
- If the error code is 102 (which occurs if the InputBox$ dialog box is canceled), the macro prevents the "Command failed" error message from being displayed.
- If the error code is 24 (which occurs if the style name does not exist), Word displays a custom message box in place of the "Bad parameter" error message, error trapping is reset with the instruction Err = 0, and the InputBox$ dialog box is displayed again so the user can type another style name. Note that Case 24 is the only case in which error trapping needs to be reset. In the other cases, the macro ends and there is no need for further error trapping.
- If the error code is neither 102 nor 24, the instruction Error Err displays the error.
Sub MAIN
On Error Goto trap
start:
apply$ = InputBox$("Type a style name:", "Apply Style")
Style apply$
trap:
Select Case Err
Case 102 'Input box was canceled
Case 24 'Bad parameter
MsgBox "No such style; try another name."
Err = 0
Goto start
Case Else 'Any other error condition
Error Err
End Select
End Sub
See Also
Error, Goto, On Error