How To Attach to a Running Instance of an Office Application

Article translations Article translations
Article ID: 238975 - View products that this article applies to.
This article was previously published under Q238975
Expand all | Collapse all

SUMMARY

To automate an Office application that is already running, you can use the GetActiveObject() API function to obtain the IDispatch pointer for the running instance. Once you have this IDispatch pointer for the running instance, you can use the methods and the properties of the running instance.

MORE INFORMATION

Automation servers register themselves in the Running Object Table (ROT) through the RegisterActiveObject() API. Automation clients can attach to a running instance with code such as the following:
      ::CoInitialize(NULL);

      // Translate server ProgID into a CLSID. ClsidFromProgID
      // gets this information from the registry.
      CLSID clsid;
      CLSIDFromProgID(L"Excel.Application", &clsid);  

      // Get an interface to the running instance, if any..
      IUnknown *pUnk;
      HRESULT hr = GetActiveObject(clsid, NULL, (IUnknown**)&pUnk);
      ASSERT(!FAILED(hr));

      // Get IDispatch interface for Automation...
      IDispatch *pDisp;
      hr = pUnk->QueryInterface(IID_IDispatch, (void **)&pDisp);
      ASSERT(!FAILED(hr));

      // Release the no-longer-needed IUnknown...
      pUnk->Release();

     // ----------------------------------------------------
     // Your automation code here-
     // ----------------------------------------------------

     ::CoUnintialize();
				
NOTE: If there are multiple instances of an automation server running at the same time, the GetActiveObject() API function returns the IDispatch pointer to the instance that was first running.

Theoretically, you can iterate the ROT for each individual instance, but Office applications do not register themselves if another instance is already in the ROT because the moniker for itself is always the same, and cannot be distinguished. This means that you cannot attach to any instance except for the first. However, because Office applications also register their documents in the ROT, you can successfully attach to other instances by iterating the ROT looking for a specific document, attaching to this document, and then getting the Application object from this document. For a code example of iterating the ROT and looking for a document name, click the article number below to view the article in the Microsoft Knowledge Base:
190985 How To Get IDispatch of an Excel or Word Document from an OCX
Note that this solution is not necessary for single-instance applications since those applications can have only one instance running at a given time. PowerPoint is an example of a single-instance application.

Properties

Article ID: 238975 - Last Review: August 18, 2005 - Revision: 3.4
APPLIES TO
  • Microsoft Office XP Developer Edition
  • Microsoft Office 2000 Developer Edition
  • Microsoft Outlook 98 Standard Edition
  • Microsoft Office 97 Standard Edition
  • Microsoft Office 95 Standard Edition
  • Microsoft Office 95a
  • Microsoft Office 95b
  • Microsoft Visual C++ 4.0 Standard Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++, 32-bit Learning Edition 6.0
Keywords: 
kbfaq kbhowto KB238975

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com