Assigning an Object to a Variable

As I mentioned at the end of Chapter 3, "Understanding Program Variables," objects have their own data types. You can declare a variable as an object by using the following form of the Dim statement: Dim variableName As ObjectType

Here, ObjectType is the data type of the object you want to work with. For example, if you want to work with a Document object, you'd use a Dim statement similar to this: Dim currentDocument As Document

It's a good idea to use object variables whenever you can because it enables you to use the Visual Basic Editor's handy IntelliSense features that I described in detail in Chapter 1 (see "Taking Advantage of IntelliSense"). Using the preceding example, if I type currentDocument and then a period (.), VBA displays a list of all the properties and methods associated with that object, as shown in Figure 5.3.

0 0

Post a comment