Article ID: 238972 - Last Review: April 19, 2007 - Revision: 8.2 Using Visual C++ to automate OfficeThis article was previously published under Q238972 On This PageSUMMARY 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 COMAutomation 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:
Three Ways to Use Automation from Visual C++There are three basic ways you can use Automation: MFC, #import, and C/C++:
181473
(http://support.microsoft.com/kb/181473/
)
How to use OLE Automation from a C application rather than C++
How to use the Office Type LibrariesA 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.Collapse this table
To do this in the 2007 Office programs, follow these steps:
Automating an Embedded Office ComponentTo 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
(http://support.microsoft.com/kb/184663/
)
How to embed and automate a Microsoft Excel worksheet with MFC
REFERENCES Chapter 24 of David Kruglinski's "Inside Visual C++"
(ISBN:1-57231-565- 2) supplies a general overview as well as some great
examples. Also, the Microsoft Knowledge Base is a good source of information.
For more information, click the following article number to view the article in the Microsoft Knowledge Base: 152023
(http://support.microsoft.com/kb/152023/
)
Locating resources to study Automation
APPLIES TO
| Article Translations
|
Back to the top
