Microsoft KB Archive/238972

= Using Visual C++ to automate Office =

Article ID: 238972

Article Last Modified on 4/19/2007

-

APPLIES TO


 * Microsoft Project 2000 Standard Edition
 * 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++ 6.0 Standard Edition
 * Microsoft Office Word 2007
 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition
 * Microsoft Word 2000 Standard Edition
 * Microsoft Word 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 Excel 2007
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition
 * Microsoft Excel 2000 Standard Edition
 * Microsoft Excel 97 Standard Edition
 * Microsoft Office Outlook 2007
 * Microsoft Office Outlook 2003
 * Microsoft Outlook 2002 Standard Edition
 * Microsoft Outlook 2000 Standard Edition
 * Microsoft Outlook 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

-



This article was previously published under Q238972



SUMMARY
You can use Automation to control Microsoft Office components from your Visual C++ applications. This article provides the basic information you need to automate Office, as well as pointers to resources and sample code.



MORE INFORMATION
Automation (formerly OLE Automation) is a technology that allows you to take advantage of an existing program's functionality and incorporate it into your own applications. For instance, you can utilize the Microsoft Word spelling and grammar checking capabilities into your application without making Microsoft Word visible to your users. You can even use all of the Microsoft Excel charting, printing, and data analysis tools. This technology can greatly simplify and speed up your development.

Automation and COM
Automation is based on the Component Object Model (COM). COM is a standard software architecture based on interfaces, and designed to separate code into self-contained objects. Think of it as an extension of the Object Oriented Programming (OOP) paradigm, but applicable to separate applications. Each object exposes a set of interfaces, and all communication to an object, such as initialization, notifications, and data transfer, happens through these interfaces.

COM is also a set of services provided by dynamic-link libraries (DLLs) installed with the operating system. Automation uses many of those services. One example is the "Marshalling" service, which packages the client application's calls to the member functions of the server application's interfaces, and passes those with their arguments to the server application. The server's interfaces appear to be exposed in the client's memory space, which is not the case when the client is an .exe file running in its own process space. Marshalling also gets the return values from the server's methods back across the process boundaries and safely into the hands of the client's call.

There are many other services essential to Automation that are provided by the various COM libraries. Sources of information about those include:
 * Inside OLE - Second Edition" by Kraig Brockschmidt, ISBN 1-55615-843-2
 * Inside COM" by Dale Rogerson - ISBN 1-57231-349-8
 * Automation Programmer's Reference," ISBN 1-57231-584-9

Three Ways to Use Automation from Visual C++
There are three basic ways you can use Automation: MFC, #import, and C/C++:
 * With MFC, use the Visual C++ ClassWizard to generate "wrapper classes" from the Microsoft Office type libraries. These classes, as well as other MFC classes, such as COleVariant, COleSafeArray, and COleException, simplify the tasks of Automation. This method is usually recommended over the others, and most of the Microsoft Knowledge Base examples use MFC.
 * #import, a new directive that became available with Visual C++ 5.0, creates VC++ "smart pointers" from a specified type library. It is very powerful, but often not recommended because of reference-counting problems that typically occur when used with the Microsoft Office applications.
 * C/C++ Automation is much more difficult, but sometimes necessary to avoid overhead with MFC, or problems with #import. Basically, you work with such APIs as CoCreateInstance, and COM interfaces such as IDispatch and IUnknown.

It is important to note that there are some slight differences between Automation from C++ compared to plain C, because COM was designed around the C++ class. For more information about automation with C, click the following article number to view the article in the Microsoft Knowledge Base:

181473 How to use OLE Automation from a C application rather than C++

How to use the Office Type Libraries
A type library is similar to a C/C++ header file. It contains the interfaces, methods, and properties that a server is publishing. You can view the type library with the OLE/COM Object Viewer (Oleview.exe) that comes with Visual C++. Following is a list of the type library file names for Microsoft Office 95, Microsoft Office 97, Microsoft Office 2000, Microsoft Office XP, Microsoft Office 2003, and 2007 Microsoft Office.

The type libraries provide you with information about the objects, methods, and properties you need to use. The best way to learn how to navigate the object models of Word, Excel, and Powerpoint is to use the Macro Recorder in the Office applications.

To do this in the 2007 Office programs, follow these steps:
 * 1) Show the Developer tab. To do this, click the Microsoft Office Button, click   Options, click Popular, click to select the Show Developer tab in the Ribbon check box, and then click OK.
 * 2) Click the Developer tab.
 * 3) In the Code group, click Record Macro, and then perform the task in which you are interested.
 * 4) In the Code group, click Stop Recording.
 * 5) As soon as you are finished recording, click Macros in the Code group, click the macro that you recorded, and then click Edit.

To do this in the Office 2003 programs and in earlier versions of Office, follow these steps:
 * 1) On the Tools menu, point to Macro, click Record New Macro, and then perform the task in which you are interested.
 * 2) On the Tools menu, point to Macro, and then click Stop Recording.
 * 3) As soon as you are finished recording, point to Macro on the Tools menu, click Macros, click the macro that you recorded, and then click Edit.

This takes you to the generated VBA code that accomplishes the task you recorded. Keep in mind that the recorded macro will not be the best possible code in most cases, but it provides a quick and usable example.

Automating an Embedded Office Component
To automate an embedded Office object or application, you need to get the object's IDispatch pointer. This is given in the Visual C++ Technical Note 39 (TN039). You can find this technical note in the Microsoft Foundation Class Library of the Visual C++ Reference. For a step-by-step example, please click the article number below to view it in the Microsoft Knowledge Base:

184663 How to embed and automate a Microsoft Excel worksheet with MFC

