Collection Objects

In programming with the Word object model (or indeed any object model), it is common to have a great many objects “alive” at the same time. For instance, each character within an open Word document is an object, as is each word, sentence, paragraph, and so on. Hence, at any given time, there may be thousands of objects in existence. To manage these objects, the designers of an object model generally include a special type of object called a collection object.

As the name implies, collection objects represent collections of objects—generally objects of a single type. For instance, the Word object model has a collection object called Characters that represents the set of all characters in the document in question. It is customary to say that the Characters collection object contains the characters in the document, so we will use this terminology as well. There is one Characters collection for each open document. Also, every time we select text within a document, Word creates another Characters collection object that contains the selected characters.

Collection objects are generally just called collections, but it is very important to remember that a collection is just a special type of object. As we will see, the properties and methods of a collection object are specifically designed to manage the collection.

We can generally spot a collection object because its name is the plural of the name of the objects contained within the collection (as with the Characters collection ...

Get Writing Word Macros, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.