All right, then. So goes the Workbook object. The Workbooks object and the Workbook object are often used to get a reference to other Excel objects of interest such as a worksheet or a range. Workbooks are opened and closed using the Workbooks object. Though you can open a workbook as easily as calling Workbooks.Open and supplying a filename, it is a much better idea to practice a little defensive programming and make sure that the file is not already open.

Speaking of the Workbooks object, this was your first crack at using collection objects. Excel has many collection objects, and you must become familiar with using them and their relationship with the underlying collection object (i.e., the relationship between the Workbooks object and the Workbook object). All collection objects have a property called Item that you can use to refer to a specific object in a collection using either a number that specifies the object's index in the collection, or the name of the object. For most collection objects, the Item property is the default property and you don't need to specify it.

Workbooks.Item("Test.xls") ' is equivalent to Workbooks("Test.xls")

The other interesting aspect of this chapter versus the prior five chapters was that you got your first taste of using events. Events are actions that an object knows how to recognize. You can place code inside event procedures to respond to these actions. This gives you an enormous amount of flexibility. This flexibility doesn't come free, however. As you use more and more events in an application, it becomes tricky to manage the interaction that occurs between them because multiple events from multiple objects can be associated with one action.

The Worksheet object is on deck. You'll use the Worksheet object much more than the Workbook object, but not as much as the Range object (which is in the hole). As you learn about the Worksheet and Range objects, you'll finally get to start outputting your results directly on worksheets rather than using the Immediate window and Debug.Print.

This page intentionally left blank

0 0

Post a comment