17.5. Dealing With Runtime Errors

Despite the help that VBA provides in eliminating language errors and compile errors, runtime errors remain an unpleasant fact of life. Sooner or later, you will get runtime errors in your code, but you don't have to take them lying down. VBA enables you to write error handlers, which are pieces of code that trap errors, analyze them, and take action if they match given error codes.

17.5.1. When Should You Write an Error Handler?

Consider writing an error handler in the following circumstances:

  • When a runtime error can cause your code to fail disastrously. For a procedure that tweaks a couple of objects on a slide in PowerPoint, you're unlikely to need an error handler. By contrast, for a procedure that creates, ...

Get Mastering VBA for Microsoft Office 2007, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.