Microsoft Office 2000 Developer   

Automating Error Handling

See Also

In an ideal world, all of the code that you write would run error-free. The reality is that, no matter how carefully you write your code, errors can and will occur. For that reason, it’s a good idea to add error-handling routines to all of your procedures.

The VBA Error Handler add-in makes it easy to add error handlers to your procedures without typing in repetitive code. It inserts standardized error handling templates into your code; you simply fill in any procedure-specific code. You can choose to add error handlers to a single procedure, to all of the procedures within a module, or to all procedures in your project at once.

To add error handlers to your code

  1. From the Add-Ins menu, select VBA Error Handler.

    Note   The VBA Error Handler menu item is only available when the VBA Error Handler add-in is loaded. For more information about loading add-ins, see Accessing Office 2000 Developer Tools.

  2. Make a selection from the Add Error Handlers To group to determine whether error handlers will be added to the current procedure, all procedures in the current module, or all procedures in the current project.

  3. Verify that the desired template is selected in the Error Handling Template box.

  4. Type the name and initials for the Author if desired. With the default template, this information appears as part of the error handler. This information is stored in the registry for future use. If you do not enter information, the name and initials default to the registered user of the product.

  5. Click OK.

The VBA Error Handler uses template files (.eht) to control the format of error handling blocks. You can edit the standard template (Errorhandler.eht) or create your own templates using Notepad or another text editor.

To update existing error handlers in your code

When selected, this option updates all existing error handlers within the scope selected in the Add Error Handlers To group so they match the currently selected template.

Note   If the Error Handler finds "On Error Goto <label>" within a procedure in existing code, no modifications are made to that code. If the Error Handler finds "On Error Goto <0>" or "On Error Resume Next" within a procedure, the code is modified to match the information specified in the template. The following comment is placed directly before the exiting On Error code:

' TODO: Turn normal error handler on when this condition is finished

For more information about the templates, see Code Commenter and Error Handler Template Formats and Chapter 11, "Add-ins, Templates, Wizards, and Libraries" in the Microsoft Office 2000/Visual Basic Programmer's Guide.