Setting References

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.

The first step in automating one Microsoft® Office application from another is referencing the application you want to automate. This reference lets your application "see" the objects exposed by the other application. In most cases, this means setting a reference to the application's type library by using the References dialog box.

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.

In your custom solutions, you must reference only 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.

You can use the objects of another Office application (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.

Tip   Because the Application object of every Office XP 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 XP object library. You might 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 XP 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 XP object library, you can use a line of code such as the following to animate the Office Assistant:

Application.Assistant.Animation = msoAnimationGreeting

**Tip   **To use the same line of code without a reference to the Microsoft® Office XP 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

**Tip   **However, this is not a recommended coding procedure, because these numbers could change during the next revision. Using the constant makes sure your code will not break because of a change in the number. To determine the values for constants such as msoAnimationGreeting, you must temporarily establish a reference to the Microsoft® Office XP 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 might not work correctly in future versions of Office. The VBA projects for all Office applications except Access include a reference to the Microsoft® Office XP office library by default. Therefore, if you want to prevent a reference to the Microsoft® Office XP 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.

See Also

Office Application Automation | Object Variable Declaration | Creation of Object Variables to Automate Another Office Application | Automating the Visual Basic Editor | The Set Statement and the New Keyword in Automation | Single-Use vs. Multi-Use Applications | Using the CreateObject and GetObject Functions | Working with Documents That Contain Startup Code | Shutting Down Objects Created by Using Automation | Using the Object Browser