The References Dialog Box

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Before you work with objects exposed by an Office application, you should set a reference to that application by using the References dialog box. To open the References dialog box, click References on the Tools menu in the Visual Basic Editor. Figure 4.3 shows the References dialog box.

Figure 4.3 The References Dialog Box

The example shown in Figure 4.3 includes references to most of the Microsoft Office applications. In your custom solutions, however, you need only reference the application that contains the objects you want to manipulate by using Automation. Including unnecessary references will increase the time it takes for your solution to load and will consume some additional memory resources.

*Note* You can use another Office application's objects (or the objects exposed by any other application or component that supports Automation) without setting a reference in the References dialog box by using the CreateObject or GetObject function and declaring object variables as the generic Object type. If you use this technique, the objects in your code will be late-bound, and as a result you will not be able to use design-time tools such as automatic statement completion or the Object Browser, and your code will not run as fast. For more information about using the CreateObject and GetObject functions, see "Using the CreateObject and GetObject Functions" later in this chapter.

*Tip* Because the Application object of every Office 2000 application includes accessor properties to work with some of the shared Office components such as the Assistant and FileSearch objects, you can work with these objects without having a reference to the Microsoft Office 9.0 object library. You may want to do this if your application must load quickly. However, when you are using a shared Office component without a reference to the Microsoft Office 9.0 object library, your code can't use enumerated constants; if it does, an error will be displayed. For example, when you are using the Assistant object with a reference to the Microsoft Office 9.0 object library, you can use a line of code such as the following to animate the Office Assistant:

Application.Assistant.Animation = msoAnimationGreeting

To use the same line of code without a reference to the Microsoft Office 9.0 office library, you must use the actual value of the msoAnimationGreeting constant, which is 2, as in the following line of code:

Application.Assistant.Animation = 2

To determine the values for constants such as msoAnimationGreeting, you must temporarily establish a reference to the Microsoft Office 9.0 office library and use the Object Browser to look up the numeric values of the constants you want work with. Using the numeric values will make your code less readable, and Microsoft doesn't guarantee that the same value will be used in future versions of Microsoft Office, so code written in this manner may not work correctly in future versions of Office. The VBA projects for all Office applications except Access include a reference to the Microsoft Office 9.0 office library by default. Therefore, if you want to prevent a reference to the Microsoft Office 9.0 office library from being loaded when your solution is opened, you must remove the reference in your solution's VBA project.

When you refer to an object in code, VBA determines what type of object it is by searching the type libraries selected in the References dialog box in the order in which they are displayed. If an object has the same name in two or more referenced type libraries, VBA uses the definition provided by the type library listed higher in the Available References list. To change the order in which the libraries are searched, you can use the Priority buttons to move the type libraries (except for the Visual Basic for Applications and the host application's type library) up or down the list. However, a better way to eliminate ambiguous object references is to fully qualify type declarations by including the programmatic identifier in front of the object name; for example, Dim docNew As Word.Document. Qualifying type declarations by using the programmatic identifier eliminates a potential source of errors and also makes your code more self-documenting.

If you have established a reference to an application or component's type library, you can learn about the exposed objects by using the Object Browser and the Help system. For more information about using the Object Browser, see "Using the Object Browser" earlier in this chapter.