Article ID: 288902 - Last Review: January 31, 2007 - Revision: 9.3 GetObject and CreateObject behavior of Office automation serversThis article was previously published under Q288902 On This PageSUMMARY This article discusses the different behaviors that occur
when you use the GetObject and CreateObject functions with various versions of Microsoft Office applications.
GetObject and CreateObject are functions that are provided by Microsoft Visual Basic and
Microsoft Visual Basic for Applications (VBA). However, the information is also
applicable to Microsoft Visual C++ if you treat references to GetObject as calls to the GetActiveObject API, and references to CreateObject as calls to the CoCreateInstance API. MORE INFORMATIONGetObjectGetObject is used to attach to a running instance of an automation server. There are a few different ways to call GetObject, but the syntax that is recommended for the Microsoft Office applications is as follows: Run-time error '429': ActiveX component can't create object You can attach to a specific instance if you know the name of an open document in that instance. For example, if an instance of Excel is running with an open workbook named Book2, the following code attaches successfully to that instance even if it is not the earliest instance that was launched: CreateObjectCreateObject is used to start a new instance of an Automation server. For example:The following table serves as a helpful reference when implementing a solution with Microsoft Office. It lists behaviors and attributes of the various versions and applications of Microsoft Office, such as whether the server defaults to being visible when launched, if it is SingleUse or MultiUse, if it has a UserControl property, if it has a Quit method, and the class name for its main window. Collapse this table
The main window class name is helpful for calling the FindWindow API when you want to find out conveniently if any instance is already running. The UserControl property is a boolean property that indicates whether the server application automatically shuts down when its last reference is released (set to nothing). The Quit method allows you to override the UserControl property in cases where it is necessary (such as when an instance does not shut down after the last reference is released). In general, Microsoft recommends that you use a new instance of an Office application instead of attaching to an instance that the user may be using. It is best create an instance by using the Application ProgID, and then open or create new objects from there. Other ProgIDs, such as Excel.Sheet and Word.Document, and so forth, are intended for use in OLE (Object linking and Embedding) and may give inconsistent results when used with CreateObject. By using the Application ProgID, you avoid potential issues by explicitly starting the server for Automation (not Embedding). When you are finished with the Automation server, release all your references to it and call its Quit method (if available) so that the server shuts down as expected. If you want to configure an instance through Automation and then leave it open for the user to use, you need to set the UserControl property to TRUE and then release all your references. The server then stays running (because the UserControl property is TRUE) and shuts down appropriately when the user closes the application (because there are no outstanding references). Note For Word, the UserControl property is read-only. It cannot be set to True or False. Word always remains running when the last reference is released. REFERENCES
For more information about using GetObject/CreateObject, and to
learn about some exceptions and issues with using Automation to Microsoft Office, click the following article numbers to view the articles in the Microsoft Knowledge Base:
222783
(http://support.microsoft.com/kb/222783/
)
PPT2000: CreateObject Does Not
Create New PowerPoint Session
188546
(http://support.microsoft.com/kb/188546/
)
BUG:
Starting Word Manually Uses Same Instance as Automation
265385
(http://support.microsoft.com/kb/265385/
)
INFO:
Automation of PowerPoint 97 and PowerPoint 2000 Viewers
259940
(http://support.microsoft.com/kb/259940/
)
BUG: Word
2000 Does Not Quit When Keybindings.ClearAll Is Called
258511
(http://support.microsoft.com/kb/258511/
)
HOWTO:
Obtain Window Handle to Office Application For Automation
249169
(http://support.microsoft.com/kb/249169/
)
PPT97:
PowerPoint 97 Remains in Memory After Getting a Presentation and Being
Closed
159922
(http://support.microsoft.com/kb/159922/
)
XL97: Using
Quit Method May Cause Illegal Operation
APPLIES TO
| Article Translations
|
Back to the top
