Share via


Visual Basic Concepts

Navigating Object Models

Once you understand how to use objects provided by components, you can use any object that is a component exposes to you. Components can range from a simple code component or ActiveX control to large components, such as Microsoft Excel and the Microsoft Data Access Object (DAO) programming interface, which expose many objects.

Each object exists somewhere in the component's object hierarchy, and you can access the objects in two ways:

  • Directly, if the object is externally creatable.

  • Indirectly, if the object is a dependent object. You can get a reference to it from another object higher in the component's hierarchy.

The best way to navigate an object hierarchy is to use the Object Browser (if the component provides an object library).

As you've seen, you navigate down an object hierarchy by setting references to dependent objects through externally creatable objects. You can also use a method on a collection object to return an individual object. For more information see "Working with Externally Creatable and Dependent Objects."

Figure 10.3 shows the object navigation path in a Microsoft Excel application.

Figure 10.3   Navigating down a Microsoft Excel object hierarchy using collections

To navigate back up, most applications use the Parent and Application, as shown in Figure 10.4.

Figure 10.4   Navigating back up a Microsoft Excel object hierarchy using the Parent and Application properties

Collection Objects

Collection objects are containers for groups of other objects. These objects provide an easy way to keep track of a set of objects that are of the same type. For example, a collection of all the Menu objects in an application can be accessed using the Menus collection object. You can use the following code to refer to all the workbooks that are currently loaded in Microsoft Excel:

Application.Workbooks

Notice that Workbooks is plural. The standard naming convention for collection objects is the plural of the type of object that makes up the collection. You can iterate through the objects in a collection by using the For Each statement, as follows:

Dim xlBook As Excel.Workbook
.
.
.
For Each xlBook In Application.Workbooks
   ' Display the name of each workbook.
   MsgBox xlBook.FullName
Next xlBook

Individual objects in many collections can also be referenced by name or by their index order in the collection. The following example shows how you would refer to Style objects named "Normal," "Example," and "Heading":

xlBook.Styles("Normal")
xlBook.Styles("Example")
xlBook.Styles("Heading")

Assuming these objects are the first three objects in the Styles, and that the collection is zero-based, you could also refer to them as follows:

xlBook.Styles(1)      ' Refers the Normal Style object.
xlBook.Styles(2)      ' Refers the Example Style
                     ' object.
xlBook.Styles(3)      ' Refers the Heading Style
                     ' object.

For More Information   For more information on working with collection objects, see "Programming with Objects."