Use Visual C ++ to automate Office

Source: Internet
Author: User

Summary
Automation and COM
Three automated methods from Visual C ++
How to use the office library
Enable embedded Office components to run automatically
Reference

I. Summary
You can use automation to control Microsoft Office Components from Visual C ++ applications. This article provides the basic information required to enable the Office to run automatically, as well as pointers to related resources and sample code.

Information Automation (formerly known as OLE Automation) is a technology that allows you to use the functions of existing programs and merge them into your own applications. For example, you can use the Microsoft Word spelling and syntax check function in the application without allowing you to view Microsoft Word. You can even use all charts, printing, and data analysis tools in Microsoft Excel. This technology can greatly simplify and speed up your development.

Ii. Automation and COM
Automation is built on the Component Object Model (COM. Com is an interface-based standard software structure designed to separate code into independent objects. It can be seen as an extension of the Object-Oriented Programming (OOP) example, but it also applies to individual applications. Each object exposes a set of interfaces through which all communication to the object (such as initialization, notification, and data transmission) is performed.

Com is also a set of services provided by the dynamic link library (DLL) installed along with the operating system. Automation uses many of these services. One example is the "sending and blocking" service, which packs client application calls into the member functions of the server application interface, then, these functions and their parameters are passed to the server application. The server interface may be exposed in the memory space of the client. However, if the client is a. EXE file and runs in its own process space, this will not happen. Sending and receiving messages can also obtain return values from the server's methods across process boundaries and securely pass these values to the client for calling.

Various com libraries provide many other services that are very important to automation. Sources of information about these services 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

 

3. Use automation methods from Visual C ++
You can use the following three basic methods for automation: MFC, # import, and C/C ++ :? With MFC, you can use visual c ++ classwizard to generate a "Packaging class" from the Microsoft Office Type Library ". These classes and other MFC classes such as colevariant, colesafearray, and coleexception simplify automated tasks. This method is better than other methods. We recommend that you use this method, and most Microsoft Knowledge Base examples use MFC.
? # Import is a new instruction introduced in Visual C ++ 5.0. It can create VC ++ "smart pointer" from a specified type library ". It is very powerful, but it is generally not recommended because it is used together with Microsoft Office applications and often causes reference counting problems.
? C/C ++ automation is much more difficult, but sometimes this method is needed to avoid overhead caused by Using MFC or to avoid problems arising when using # import. Basically, you will use APIs such as cocreateinstance () and COM APIs such as idispatch and iunknown.
It must be noted that the automation in C ++ is slightly different from the automation in General C, because COM is designed around the C ++ class. For other information about automation using C, click the following article number to view the article in the Microsoft Knowledge Base:
181473 (http://support.microsoft.com/kb/181473/) how to use OLE Automation in C applications rather than in C ++

4. How to use the office library
The Type Library is similar to the C/C ++ header file. It contains the interfaces, methods, and attributes published by the server. You can use the OLE/COM Object viewer (oleview.exe) attached to Visual C ++ to view the Type Library. The file names of the types of libraries Microsoft Office 95, Microsoft Office 97, Microsoft Office 2000, and Microsoft Office XP are listed below:

Office Application Type Library
Word 95 and earlier wb70en32. TLB
Excel 95 and earlier versions xl5en32. olb
PowerPoint 95 and earlier versions. TLB
Access 95 and earlier versions of msaccess. TLB
Binder 95 binder. TLB
Schedule + sp7en32. olb
Project pj4en32. olb
Team Manager m1_gr1.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

These types of libraries provide you with information about the objects, methods, and attributes to be used. The best way to learn how to navigate the Word, Excel, and PowerPoint object models is to use the macro recorder in these office applications:

1. Select recording new macros from the macro options on the tool menu and execute the tasks you are interested in.
2. Choose "stop recording" from the macro option on the tool menu.
3. After recording, select a macro from the macro options on the tool menu, select the macro you recorded, and click Edit.
You will see the generated VBA code, which can complete the task you have recorded. Remember, the recording macro is not the best code in most cases, but it can provide quick and available examples.

5. Make the embedded office component run automatically
To make the embedded office object or application run automatically, You need to obtain the idispatch pointer of the object. This pointer is provided in Visual C ++ technical note 39 (tn039. You can find this technical description in the Microsoft basic class library in Visual C ++ reference. To obtain a step-by-step example, click the following article number to view the article in the Microsoft Knowledge Base:
184663 (http://support.microsoft.com/kb/184663/) how to use MFC to embed and automate Microsoft Excel worksheets

Reference
Chapter 24th of David kruglinski's "inside visual c ++" (ISBN: 1-57231-565-2) outlines this content and provides some good examples. The Microsoft Knowledge Base is also a good source of information.

For other information, click the following article number to view the article in the Microsoft Knowledge Base:
152023 (http://support.microsoft.com/kb/152023/) find resources for learning automation
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.