Returning an Object from a Collection

Returning an Object from a Collection

The Item method returns a single object from a collection. The following example sets the firstDoc variable to a Document object that represents the first document in the Documents collection.

Set firstDoc = Documents.Item(1)

The Item method is the default method for most collections, so you can write the same statement more concisely by omitting the Item keyword.

Set firstDoc = Documents(1)

Named Objects

Although you can usually specify an integer value with the Item method, it may be more convenient to return an object by name. The following example switches the focus to a document named Sales.doc.

Documents("Sales.doc").Activate

The following example selects the range of text marked by the bookmark named temp in the active document.

ActiveDocument.Bookmarks("temp").Select

Not all collections can be indexed by name. To determine the valid collection index values, see the collection object topic.

Predefined Index Values

Some collections have predefined index values you can use to return single objects. Each predefined index value is represented by a constant. For example, you specify an WdBorderType constant with the Borders property to return a single Border object.

The following example adds a single 0.75 point border below the first paragraph in the selection.

With Selection.Paragraphs(1).Borders(wdBorderBottom)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth075pt
End With