Share this Page URL

A VBA Language Primer > Error Handling - Pg. 424

Through each iteration of the loop, subsequent Document objects are assigned to the objDoc loop variable, which does your work. In our first looping example, we used the Item property and an index (the loop counter in this case) to indicate which recipient to grab from the collection. For collections in Word VBA, the Item property is the default property . This means that we can forgo querying the Item property and just type in an index after the collection variable (Example 18-26). Example 18-26. Using an Index Instead of the Item Property Set objDoc = Documents(intCounter) Some collections (like the Documents collection) also support using a "key" string to reference an object in a collection. For instance, you can fetch a particular document by name (Example 18-27). Example 18-27. Using a Key String to Reference an Object Set objDoc = Documents("C:\MyDoc.doc") Error Handling Ideally, your code would run without any problems. But errors happen. Normally, a VBA program stops running and a message box is displayed when an error occurs. By using the On Error statement, you can replace this default error message with