In practical applications, programmers often like the program to automatically generate Word description documents, explain the status of the program running or the results of the operation, or the program can extract the contents of the database to generate a word form, so that users can easily view and modify, print. But VC + + call Word is really not easy, especially for the use of various functions in Word, this article with the author's work experience in detail how to call word and fill in word form, have the same needs of friends can also look at the MSDN Automation Microsoft Office and Microsoft Office 2000. The following is an example of Microsoft Office Word.
Type library
A type library is a part of a file or file that provides information about the functionality of a COM object, and the type library contains information about the class. Note that the type library does not store the actual objects, but simply stores information about those objects. The type library details information about the methods and attributes that an Automation client needs to invoke for an object, such as a detailed description of the values that are accepted or returned.
Each Microsoft Office application provides a variety of type library resources in a DLL file called the target library (*.olb). The following table lists the names of Microsoft Office 97 and Microsoft Office 2000 type library files.
Microsoft Access |
Msacc8.olb |
Msacc9.olb |
Microsoft Excel |
Excel8.olb |
Excel9.olb |
Microsoft Graph |
Graph8.olb |
Graph9.olb |
Microsoft Outlook® |
Msoutl8.olb Note:use Msoutl85.olb for Outlook 98 |
Msoutl9.olb |
Microsoft powerpoint® |
Msppt8.olb |
Msppt9.olb |
Microsoft Word |
Msword8.olb |
Msword9.olb |
|
In VC + + call Word, we need to import Msword9.olb to use the various features in Word.
Create an automated client with VC + +
The COleDispatchDriver Class
VC provides a COleDispatchDriver class to handle the IDispatch interface of the automation object, and the properties and functions of the Coledispatchdrive class please refer to the class library for your own reference.
1. Create a new dialog-base MFC AppWizard EXE project named "Wordautomation", note the check on the Automaiton option in MFC Appwizard-step 2 of 4, as shown in the following figure:
Figure one uses AppWizard to generate the project based on the dialog box, as shown above automation option.
2. In the View menu, click ClassWizard, then enter the Automation tab and click Add Class to select from A Type Library. Locate the Microsoft Office 2000 type library Msword.olb, select _application,_document,_documents,selection Four classes, and if you have other needs (such as drawing a table, you can also select a different Class).
Figure II using ClassWizard to create the desired class from the type library