In VC ++, word is called to fill in the Word Table.

Source: Internet
Author: User
Tags microsoft outlook

From: http://www.vckbase.com/document/viewdoc? Id = 1186

Download source code

In practical applications, programmers often likeProgramThe system can automatically generate WORD instructions to indicate the program running status or running results. Alternatively, the program can extract the database content to generate a Word Table, allowing you to conveniently view, modify, and print the table. However, it is not easy to call word in VC ++, especially for the use of various functions in Word. This article describes in detail how to call Word and fill in word forms based on the author's work experience, if you have the same requirements, you can view automation Microsoft Office 97 and Microsoft Office 2000 in msdn. The following uses Microsoft Office 2000 word as an example.

Type Library
A Type Library is a part of a file or file that provides COM Object Function information, and the Type Library contains information about classes. Note: The Type Library does not store actual objects, but only stores information about these objects. The Type Library details the methods and attributes that an automated client needs to call for an object. For example, it describes the values that are accepted or returned in detail.
Each Microsoft Office application provides multiple types of library resources in a DLL file, which is called the target Library (*. olb ). The following table lists the names of the Microsoft Office 97 and Microsoft Office 2000 library files.

Application Version 97 (or 8.0) Version 2000 (or 9.0)
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 ++ to call word, we need to import msword9.olb to use various functions in word.

Create an automatic client with VC ++

The coledispatchdriver class


VC provides a coledispatchdriver class to process the idispatch interface of automation objects. For the attributes and functions of the coledispatchdrive class, read the class library by yourself.
1.Create a new dialog-base MFC Appwizard EXE project named "wordautomation". Check the Automaiton option in MFC Appwizard-Step 2 of 4, as shown in:


Figure 1 uses Appwizard to generate a dialog box-based project. Select the automation option as shown above.

2.In the View menu, click classwizard, enter the automation label, click Add class, and select from a type library. Find the Microsoft Office 2000 library MSWord. olb, and select four classes: _ application, _ document, _ documents, and selection. If you want to create a table, select another class ).


Figure 2 Use classwizard to create the required class from the Type Library

This process creates two new files in the project: msword8.h and msword8.cpp. These files constitute the selected class and member functions of the class of the word library. View all the classes generated from the word library in the classview view, and double-click the _ application class to view its definition. You will notice that the _ application class comes from the coledispatchdriver.

 
// _ Application wrapper classclass _ application: Public coledispatchdriver {......}

As for the functions and usage of these classes and member functions, I have a simple method, that is, using the macro recording function in the tool menu in word, first record the operations you want, and then view these macros.Code, You will know which class to use, which member functions and member functions should contain some parameters. Although these codes are written in VB, you can easily convert them into codes in VC ++.


Figure 3 VBA macro code written in Word

VC ++ automatically generates the following code in cwordautomationapp: initinstance () in wordautomation. cpp to make the COM Service effective.

 
If (! Afxoleinit () {afxmessagebox (idp_ole_init_failed); Return false ;}

3.Next, select the resource idd_wordautomation_dialog in the dialog box, and add a button named idc_word_new in the dialog box. Add the following code to the processing function:

Colevariant vtrue (short) True), vfalse (short) False), vopt (long) disp_e_paramnotfound, vt_error ); // start a new Microsoft Word 2000 instance _ application owordapp; If (! Owordapp. createdispatch ("word. application ", null) {afxmessagebox (" createdispatch failed. ", mb_ OK | mb_setforeground); return;} // create a new word document named documents odocs; _ document odoc; odocs = owordapp. getdocuments (); odoc = odocs. add (vopt, vopt); // if it is Word 98, it should contain two parameters, such as odocs. add (vopt, vopt) // Add the text to the Word document Selection Osel; Osel = owordapp. getselection (); Osel. typetext ("one"); Osel. typeparagraph (); Osel. typetext ("two"); Osel. typeparagraph (); Osel. typetext ("three"); // Save the Word document _ document oactivedoc; oactivedoc = owordapp. getactivedocument (); oactivedoc. saveas (colevariant ("C :\\ doc1.doc"), colevariant (short) 0), vfalse, colevariant (""), vtrue, colevariant (""), vfalse, vfalse, vfalse); // exit the word application owordapp. quit (vopt, vopt, vopt );

4.Add the header file msword9.h to woreautomation. cpp.

 
# Include "msword9.h" // or "msword8.h" in word98"

Note: The added header file should be after the stdafx. h file; otherwise, compilation errors may occur.

Use VC ++ to fill out word forms

We first use the macro recording function of word to record the operation of filling in the form. The code for viewing VBA is as follows:

Figure 4 VBA macro code for entering the Word Table

Among them, movedown is the member function of the object defined by the selection class, unit: = wdparagraph, Count: = 1 is the value of the parameter it carries. The value of the wdparagraph macro can be found through the macro debugger during debugging. In VC ++, there are three movedown parameters. In VBA help, we can find that there is a default value for the third parameter, but this parameter cannot be omitted in VC ++, how can we know the value? We can write it into the macro according to the gourd painting, extend: = wdmove, then find it in debugging, and so on.
Select the resource idd_wordautomation_dialog in the dialog box and add a button named idc_word_tablewrite in the dialog box. Add the following code to the processing function:

Beginwaitcursor (); colevariant vtrue (short) True), vfalse (short) False), vopt (long) disp_e_paramnotfound, vt_error); _ application m_app; // define the application object provided by the word; Documents m_docs; // define the Document Object provided by the word; selection m_sel; // define the selection object provided by the word; m_docs.releasedispatch (); m_sel.releasedispatch (); m_app.m_bautorelease = true; If (! M_app.createdispatch ("word. application") {afxmessagebox ("An error occurred while creating the Word2000 service! "); Exit (1) ;}// the variant variable is defined below; colevariant varfilepath (" word .doc "); colevariant varstrnull (" "); colevariant varzero (short) 0); colevariant vartrue (short (1), vt_bool); colevariant varfalse (short (0), vt_bool); m_docs.attachdispatch (m_app.getdocuments ()); // associate the values object m_docs and idispatch interfaces; m_docs.open (varfilepath, varfalse, varstrnull, varfalse, varstrnull, varstrnull, vartrue, vart Rue, vartrue); // open the Word document; m_sel.attachdispatch (m_app.getselection (); // associate the selection Class Object m_sel with the idispatch interface; m_sel.movedown (colevariant (short) 4 ), colevariant (short) 1), colevariant (short) 0); m_sel.movedown (colevariant (short) 5), colevariant (short) 1), colevariant (short) 0); m_sel.typetext ("123456789"); m_sel.moveright (colevariant (short) 1), colevariant (short) 1), colevariant (short) 0 )); m_sel.typetext ("Li Ming "); M_sel.moveright (colevariant (short) 1), colevariant (short) 1), colevariant (short) 0); m_sel.typetext ("25 "); m_sel.moveright (colevariant (short) 1), colevariant (short) 1), colevariant (short) 0); m_sel.typetext ("technician "); m_sel.moveright (colevariant (short) 1), colevariant (short) 1), colevariant (short) 0); m_sel.typetext ("Undergraduate "); m_sel.moveright (colevariant (short) 1), colevariant (short) 1), colevariant (short) 0); M_s El. typetext ("No. 315 Minzhu Road, Hongkou District, Shanghai"); // save word file_document oactivedoc; oactivedoc = m_app.getactivedocument (); oactivedoc. saveas (colevariant ("C :\\ .doc"), colevariant (short) 0), vfalse, colevariant (""), vtrue, colevariant (""), vfalse, vfalse, vfalse); m_docs.releasedispatch (); // disconnect; m_sel.releasedispatch (); // exit word m_app.quit (vopt, vopt, vopt); m_app.quit (vopt, vopt, vopt); m_app.rel Easedispatch (); endwaitcursor (); MessageBox ("the word form is complete! "," Prompt ", mb_iconexclamation );

Note:: To run the source program provided in this article, you must install Microsoft Word 2000 in advance. Figure 4 VBA macro code for entering the Word Table

Among them, movedown is the member function of the object defined by the selection class, unit: = wdparagraph, Count: = 1 is the value of the parameter it carries. The value of the wdparagraph macro can be found through the macro debugger during debugging. In VC ++, there are three movedown parameters. In VBA help, we can find that there is a default value for the third parameter, but this parameter cannot be omitted in VC ++, how can we know the value? We can write it into the macro according to the gourd painting, extend: = wdmove, then find it in debugging, and so on.
Select the resource idd_wordautomation_dialog in the dialog box and add a button named idc_word_tablewrite in the dialog box. Add the following code to the processing function:

Beginwaitcursor (); colevariant vtrue (short) True), vfalse (short) False), vopt (long) disp_e_paramnotfound, vt_error); _ application m_app; // define the application object provided by the word; Documents m_docs; // define the Document Object provided by the word; selection m_sel; // define the selection object provided by the word; m_docs.releasedispatch (); m_sel.releasedispatch (); m_app.m_bautorelease = true; If (! M_app.createdispatch ("word. application") {afxmessagebox ("An error occurred while creating the Word2000 service! "); Exit (1) ;}// the variant variable is defined below; colevariant varfilepath (" word .doc "); colevariant varstrnull (" "); colevariant varzero (short) 0); colevariant vartrue (short (1), vt_bool); colevariant varfalse (short (0), vt_bool); m_docs.attachdispatch (m_app.getdocuments ()); // associate the values object m_docs and idispatch interfaces; m_docs.open (varfilepath, varfalse, varstrnull, varfalse, varstrnull, varstrnull, vartrue, vart Rue, vartrue); // open the Word document; m_sel.attachdispatch (m_app.getselection (); // associate the selection Class Object m_sel with the idispatch interface; m_sel.movedown (colevariant (short) 4 ), colevariant (short) 1), colevariant (short) 0); m_sel.movedown (colevariant (short) 5), colevariant (short) 1), colevariant (short) 0); m_sel.typetext ("123456789"); m_sel.moveright (colevariant (short) 1), colevariant (short) 1), colevariant (short) 0 )); m_sel.typetext ("Li Ming "); M_sel.moveright (colevariant (short) 1), colevariant (short) 1), colevariant (short) 0); m_sel.typetext ("25 "); m_sel.moveright (colevariant (short) 1), colevariant (short) 1), colevariant (short) 0); m_sel.typetext ("technician "); m_sel.moveright (colevariant (short) 1), colevariant (short) 1), colevariant (short) 0); m_sel.typetext ("Undergraduate "); m_sel.moveright (colevariant (short) 1), colevariant (short) 1), colevariant (short) 0); M_s El. typetext ("No. 315 Minzhu Road, Hongkou District, Shanghai"); // save word file_document oactivedoc; oactivedoc = m_app.getactivedocument (); oactivedoc. saveas (colevariant ("C :\\ .doc"), colevariant (short) 0), vfalse, colevariant (""), vtrue, colevariant (""), vfalse, vfalse, vfalse); m_docs.releasedispatch (); // disconnect; m_sel.releasedispatch (); // exit word m_app.quit (vopt, vopt, vopt); m_app.quit (vopt, vopt, vopt); m_app.rel Easedispatch (); endwaitcursor (); MessageBox ("the word form is complete! "," Prompt ", mb_iconexclamation );

note : To run the source program provided in this article, you must install Microsoft Word 2000 in advance.

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.