Microsoft Knowledge Base Email Alertz

KBAlertz.com: (288902) - 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...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 288902 - Last Review: January 31, 2007 - Revision: 9.2

GetObject and CreateObject behavior of Office automation servers

This article was previously published under Q288902

On This Page

SUMMARY

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 INFORMATION

GetObject

GetObject 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:
set xlApp = GetObject(, "Excel.Application")
				
If an instance of Microsoft Excel is running when this code is executed, you have access to the running instance's object model through the xlApp variable. If no instance is running, you receive the following trappable run-time error message:
Run-time error '429':
ActiveX component can't create object
If multiple instances of Microsoft Excel are running, GetObject attaches to the instance that is launched first. If you then close the first instance, another call to GetObject attaches to the second instance that was launched, and so forth.

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:
Set xlApp = GetObject("Book2").Application
				

CreateObject

CreateObject is used to start a new instance of an Automation server. For example:
set xlApp = CreateObject("Excel.Application")
				
Depending on whether the server is designed as SingleUse or MultiUse, another server process may or may not be launched. This might be an important distinction for deciding whether you should forcibly shut down an Automation instance. For example, with a MultiUse server, if an instance is already running before you attach to it, then you might want to avoid shutting down the server programmatically when you are done automating it.

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 tableExpand this table
Application(s)VisibleInstancingHas UserControlHas QuitClassName
Excel 97, 2000, 2002, 2003, 2007NoSingleUseYesYesXlMain
Word 97, 2000, 2002, 2003, 2007NoSingleUseYesYesOpusApp
PowerPoint 97NoMultiUseNoYesPP97FrameClass
PowerPoint 2000NoMultiUseNoYesPP9FrameClass
PowerPoint 2002NoMultiUseNoYesPP10FrameClass
PowerPoint 2003NoMultiUseNoYesPP11FrameClass
PowerPoint 2007NoMultiUseNoYesPP12FrameClass
Access 97YesSingleUseYesYesOMain
Access 2000, 2002, 2003, 2007NoSingleUseYesYesOMain
Project 98, 2000NoMultiUseYesYesJWinproj-WhimperMainClass

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://kbalertz.com/Feedback.aspx?kbNumber=222783/ ) PPT2000: CreateObject Does Not Create New PowerPoint Session
188546  (http://kbalertz.com/Feedback.aspx?kbNumber=188546/ ) BUG: Starting Word Manually Uses Same Instance as Automation
265385  (http://kbalertz.com/Feedback.aspx?kbNumber=265385/ ) INFO: Automation of PowerPoint 97 and PowerPoint 2000 Viewers
259940  (http://kbalertz.com/Feedback.aspx?kbNumber=259940/ ) BUG: Word 2000 Does Not Quit When Keybindings.ClearAll Is Called
258511  (http://kbalertz.com/Feedback.aspx?kbNumber=258511/ ) HOWTO: Obtain Window Handle to Office Application For Automation
249169  (http://kbalertz.com/Feedback.aspx?kbNumber=249169/ ) PPT97: PowerPoint 97 Remains in Memory After Getting a Presentation and Being Closed
159922  (http://kbalertz.com/Feedback.aspx?kbNumber=159922/ ) XL97: Using Quit Method May Cause Illegal Operation

APPLIES TO
  • Microsoft Office Excel 2007
  • Microsoft Office Excel 2003
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Office Access 2007
  • Microsoft Office Access 2003
  • Microsoft Access 2002 Standard Edition
  • Microsoft Access 2000 Standard Edition
  • Microsoft Access 97 Standard Edition
  • Microsoft Office PowerPoint 2007
  • Microsoft Office PowerPoint 2003
  • Microsoft PowerPoint 2002 Standard Edition
  • Microsoft PowerPoint 2000 Standard Edition
  • Microsoft PowerPoint 97 Standard Edition
  • Microsoft Project 2000 Standard Edition
  • Microsoft Project 98 Standard Edition
  • Microsoft Office Word 2007
  • Microsoft Word 2002
  • Microsoft Word 2000
  • Microsoft Word 97 Standard Edition
Keywords: 
kbautomation kbinfo KB288902
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please