Share this Page URL

The Application Object > The Application Object - Pg. 425

Example 18-29. Using the DoLoop procedure Private Sub DoLoopCarefully( On Error Goto Loop_Err ) For Each objDoc in Documents MsgBox objDoc.BuiltInDocumentProperties(wdPropertyKeywords) Next Exit Sub Loop_Err: MsgBox "Error " & Err.Number & ": " & Err.Description End Sub If we encounter an error in the DoLoopCarefully procedure, execution will continue at the Loop_Err label at the end of the procedure body. This part of the procedure is called an error handler . The error handler in this example is retrieving properties from an object called Err . This is VBA's built-in error object. Here, we're using it to display an error number and a description of the error. You should also note the Exit Sub statement after the loop. At this point, the code is done, and you need to exit. If you don't, you fall into the error handler and get a bogus error message.