Referring to Another Application

When you refer to another application by using OLE Automation, you actually refer to one of the application's top-level objects. The task you want to accomplish determines the object you refer to. The following table shows the most common top-level objects for Microsoft Project and Word; for a more complete list of the top-level objects in Office applications, see the Microsoft Solutions Development Kit.


To return

From this application

Use this
class name

A reference to the Microsoft Project Application object, which allows you to use all of Microsoft Project's objects, properties, and methods

Microsoft Project

MSProject.Application

A reference to a new Project object or a reference to the Project object specified in the GetObject method, which allows you to work with a specific project

Microsoft Project

MSProject.Project

A reference to the WordBasic object, which allows you to run WordBasic statements

Word

Word.Basic


There are two ways to refer to a top-level object using OLE Automation from Microsoft Excel. One way is to refer to the application's object library using the References dialog box.

To establish a reference to an application's object library

1. Switch to a Visual Basic module.

2. On the Tools menu, click References.

3. Click the object library you want to refer to.

If the object library you want doesn't appear in the Available References box, click Browse and then locate the file you want.

After you've established a reference to an object library, you can use commands from the library in your Microsoft Excel Visual Basic code. For example, after you've established a reference to the Word object library, you can use a command such as FileOpen. However, referring to objects this way can yield unpredictable results, because you cannot always be sure which object will be returned. The preferred way is either to use the CreateObject method to start the application and return a reference to a top-level object or to use the GetObject method to return a reference to a top-level object from an application that's already running. For more information about these methods, see the following sections.

Note

An object library is a catalog of an application's objects, properties, and methods. Any application that registers object libraries in the Windows system registry allow other applications to use its objects, properties, and methods as if they were native to that application.

Word exposes three top-level objects: the Application object, the CurValues object, and the WordBasic object. The WordBasic object is the most commonly used of the three because you execute all WordBasic commands by first referring to this object.

The CreateObject Method

The CreateObject method starts a new instance of an application invisibly and returns a reference to a new top-level object. The following code loads Microsoft Project into memory and returns the Microsoft Project Application object.


Dim projapp As Object

Set projapp = CreateObject("MSProject.Application")

The following example returns a Project object instead of the Application object, specifying the Project class name in the CreateObject argument.


Dim projapp As Object

Set projapp = CreateObject("MSProject.Project")

The GetObject Function

The GetObject function returns a top-level object reference from an application that's is already running. The pathName argument can be either the complete path to an existing file or an empty string, or it can be omitted altogether. If you omit this argument, you must supply the class argument; the function will then create a new instance of the application. An error will be returned if the path you specify doesn't exist. For more information about the syntax for the GetObject function, see "GetObject function" in Help.

The following code returns the Microsoft Project Application object from an instance of Microsoft Project that's already loaded into memory. You can return a Project object instead by specifying the Project class name in the GetObject argument.


Dim projapp As Object

Set projapp = GetObject("", "MSProject.Application")