Working with Object Collections

     

A collection is a set of similar objects. For example, Word's Documents collection is the set of all the open Document objects. Similarly, the Paragraphs collection is the set of all Paragraph objects in a document. Collections are objects, too, so they have their own properties and methods, and you can use these properties and methods to manipulate one or more objects in the collection.

graphics/note_icon.gif

It's important to understand that you often can't refer to objects by themselves . Instead, you must refer to the object as an element in a collection. For example, when referring to the Budget.doc document, you can't just use Budget.doc . You have to use Documents("Budget.doc") so that VBA knows you're talking about a currently open document.


The members of a collection are called the elements of the collection. You can refer to individual elements using either the object's name or an index. For example, the following statement closes a document named Budget.doc :

 Documents("Budget.doc").Close 

On the other hand, the following statement uses an index to select the first Bookmark object in the active document:

 ActiveDocument.Bookmarks(1).Select 

If you don't specify an element, VBA assumes you want to work with the entire collection.



Absolute Beginner's Guide to VBA
Absolute Beginners Guide to VBA
ISBN: 0789730766
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net