Share via


Automating Error Handling

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

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 is 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.

  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

  • In the VBA Error Handler add-in, select the Update existing error handlers option.

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

See Also

Writing Error-Free Code | Design-Time Tools | Run-Time Tools | Script Debugging Tools | Basic Error Handling | Getting Information About an Error | Error Handling in Class Modules | Handling Script Errors | Logging Errors