You can think of the object model as a road map. To get from town A to town B, you can't simply drive across fields, through forests and rivers to get to your destination. You must take designated highways, and you leave the highway at the most convenient interchange and take a minor road to the place you are going. The same is true of an object hierarchy. You must use the paths laid down by the designer of the object model to navigate from one level of the hierarchy to the next, and so on. And just like a road system, there is often more than one route to get to the same place, but one may be quicker and more efficient than another.
One of the most common ways to obtain a reference to an object on the next level down in an object model hierarchy is through a property that returns an object. For example, a property of Excel's Worksheet object is Cells, which in fact returns a Range object. You must therefore understand what type of object is returned by a particular property or method in order to declare a local object variable of the correct type. For example:
Dim oXLapp As Excel.Application Dim oXLwsh As Excel.Worksheet Dim oXLrng As Excel.Range
Set oXLapp = New Excel.Application Set oXLwsh = oXLapp.Worksheets.Add Set oXLrng = oXLwsh.Cells
82 Chapter 5 - Automation
There are many occasions when you are working with a particular object and need to call a method or property belonging to an object directly above it in the hierarchy. For this purpose, most object hierarchies provide you with a means of navigating back up the object model. This not only saves time when coding, but it's much more efficient at runtime. While the implementation is different in each object model, you should be on the lookout for properties called Parent, Window, Application, or Top. These are the properties that return a reference to objects further up the hierarchy.
Was this article helpful?