Using Visual C++ to Automate Office

 
INFO: Using Visual C++ to Automate Office
View products that this article applies to.
Article ID : 238972
Last Review : March 23, 2006
Revision : 7.0
This article was previously published under Q238972
On This Page
 
SUMMARY
 
MORE INFORMATION
 
Automation and COM
 
Three Ways to Use Automation from Visual C++
 
How to use the Office Type Libraries
 
Automating an Embedded Office Component
 
REFERENCES
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.
 Back to the top
 
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.
 Back to the top
 
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
 
 Back to the top
 
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 additional information about automation with C, click the following article number to view the article in the Microsoft Knowledge Base:
181473 (http://support.microsoft.com/kb/181473/) HOWTO: Use OLE Automation from a C Application Rather Than C++
 Back to the top
 
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, and Microsoft Office XP:
 
Office Application Type library
Word 95 and prior wb70en32.tlb
Excel 95 and prior xl5en32.olb
PowerPoint 95 and prior PowerPoint.tlb
Access 95 and prior msaccess.tlb
Binder 95 binder.tlb
Schedule+ sp7en32.olb
Project pj4en32.olb
Team Manager mstmgr1.olb
Word 97 msword8.olb
Excel 97 excel8.olb
PowerPoint 97 msppt8.olb
Access 97 msacc8.olb
Binder 97 msbdr8.olb
Graph 97 graph8.olb
Outlook 97 msoutl8.olb
Outlook 98 msoutl85.olb
Word 2000 msword9.olb
Excel 2000 excel9.olb
PowerPoint 2000 msppt9.olb
Access 2000 msacc9.olb
Outlook 2000 msoutl9.olb
Word 2002 msword.olb
Excel 2002 excel.olb
PowerPoint 2002 msppt.olb
Access 2002 msacc.olb
Outlook 2002 msoutl.olb
Office Word 2003 msword.olb
Office Excel 2003 excel.exe
Office PowerPoint 2003 msppt.olb
Office Access 2003 msacc.olb
Office Outlook 2003 msoutl.olb
 
 
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 these Office applications: 1. Choose Record New Macro from the Macro option on the Tools menu and execute the task you're interested in.
2. Choose Stop Recording from the Macro option on the Tools menu.
3. Once you are done recording, choose Macros from the Macro option on the Tools menu, select the macro you recorded, 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.
 Back to the top
 
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 (http://support.microsoft.com/kb/184663/) HOWTO: Embed and Automate a Microsoft Excel Worksheet with MFC
 Back to the top
 
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 additional 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
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值