Manipulate MS word 123 in Visual C ++

Source: Internet
Author: User
Tags microsoft outlook

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

Use VC programming to manipulate the office. You can print Word files, send data to Word documents, send e-mail, automatically generate tables, Excel data statistics, and pie charts, histogram display, automatic report generation, playback slides, Doc, txt, HTML, and RTF file conversion, simplified and Traditional Chinese conversion, Pinyin or stroke sorting ...... all functions that can be implemented by office can be called in programs you write. Read the following instructions carefully and download the source file for reference. you can master this technology step by step. I wish my friends a happy learning experience.

I. Concepts
Microsoft Office products provide interfaces for OLE Automation automation programs. If you use VB, VBA, and script scripts to call the office function, it is much easier to call than to use VC. For example, in word, you can call up the menu "tool (T)/macro (m)/recording new macro (r)". At this time, it starts to record any menu and keyboard operations in word, save your operation process so that you can call it again. The VBA program (Visual Basic for Application) is used to save the records of these operations ). The following functions also need to be implemented by referring to the VBA method.

2. Structure Level
To operate the Office more logically and hierarchically, Microsoft divides the application into the following tree structure based on the logic function:

Application (word is used as an example to list only a part)
Documents (all documents)
Document (one document)
......
Templates (all templates)
Template (a template)
......
Windows (all windows)
Window
Selection
View
Selection (Edit object)
Font
Style
Range
......
......

Only by understanding the logical hierarchy can we manipulate the Office correctly. For example, if a VBScript statement is:
Application. activedocument. saveas "C:/abc.doc"
Then, we will know that the operation process is: Step 1, get application; Step 2, get activedocument from application; step 3, call the document function saveas, A parameter is a string file name.

3. Basic Steps
(1) create (or open existing) an MFC Program
(2) Ctrl + W execute classwizard (this article follows the vc6 operation, the example program is also written and tested under vc6)
(3) add class.../from a type library... in the office directory, find the type library you want to use. (I am using Office2000, whose word library files are saved in C:/program files/Microsoft Office/office/msword9.olb.) according to your office version, you can use the type library files listed in the following table.

Office version and type

Type Library File

Office version and type

Type Library File

Access 97 Msacc8.olb PowerPoint 2000 Msppt9.olb
Jet Database 3.5 dao350.dll Word 2000 Msword9.olb
Binder 97 Msbdr8.olb Access 2002 Msacc. olb
Excel 97 Excel8.olb Excel 2002 Excel.exe
Graph 97 Graph8.olb Graph 1, 2002 Graph.exe
Office 97 Mso97.dll Office 2002 MSO. dll
Outlook 97 Msoutl97.olb Microsoft Outlook 2002 Msoutl. olb
Powerpoint 97 Msppt8.olb PowerPoint 2002 Msppt. olb
Word 97 Msword8.olb Word 2002 MSWord. olb
Access 2000 Msacc9.olb Office Access 2003 Msacc. olb
Jet Database 3.51 Dao360.dll Microsoft Office Excel 2003 Excel.exe
Binder 2000 Msbdr9.olb Graph 1, 2003 Graph.exe
Excel 2000 Excel9.olb Office 2003 MSO. dll
Graph 1, 2000 Graph9.olb Microsoft Office Outlook 2003 Msoutl. olb
Office 2000 Mso9.dll Office PowerPoint 2003 Msppt. olb
Microsoft Outlook 2000 Msoutl9.olb Office Word 2003 MSWord. olb

(4) After selecting the type library file, continue to select the class to be added in the pop-up dialog window. The specific class to choose depends on what functions you plan to call in the program in the future. Of course, you do not need to consider so much. You can use the mouse and shift key to select all options.
(5) initialize COM. Method 1: Find the initinstance () function of the app and add the afxoleinit () function to it. Method 2: coinitialize (null) where the com function needs to be called ), couninitialize () after the call is completed ().
(6) In the CPP file where you need to call the office function
# Include <atlbase. h> // use the ccomvariant template class to facilitate variable operations of the variant type.
# Include "header file. H" // The specific header file name is determined by the file name of the load type library. (You can see the double-point packaging file)
// For example, if msword9.olb is used, the header file is msword9.h
(7) Well, start writing programs now. In addition, Steps 3 and 4 can also use the # import method to introduce the Type Library.

IV. Implementation skills
In the process of calling the office function, the most difficult thing is to determine the function parameters. Generally, the parameters are variable pointers of the variant type. So how should we write it? Two methods are recommended. One is to read books about VBA, and the other is to use the built-in "macro" function in office. We strongly recommend that you use the second method to record the functions you want to complete in the office operating environment using macros, and then observe and analyze the recorded functions and parameters, it can be used in VC. For example:

      ActiveDocument.SaveAs FileName:="Hello.doc", FileFormat:=wdFormatDocument _        , LockComments:=False, Password:="", AddToRecentFiles:=True, _        WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _         SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _        False

The above is a macro for saving files recorded in word, and the function prototype in VC is

      void _Document::SaveAs(VARIANT* FileName, VARIANT* FileFormat, VARIANT* LockComments,        VARIANT* Password, VARIANT* AddToRecentFiles, VARIANT* WritePassword,         VARIANT* ReadOnlyRecommended, VARIANT* EmbedTrueTypeFonts, VARIANT* SaveNativePictureFormat, VARIANT* SaveFormsData, VARIANT* SaveAsAOCELetter)

After comparison, we can see that the filename parameter is a string variant (vt_bstr), The lockcomments parameter is Boolean variant (vt_bool), and so on. Parameter fileformat: = what type is wdformatdocument? In fact, this is a constant that specifies the file type when saving, and it is obviously a DWORD type variant (vt_i4 ). So what is the constant value? It's easy. Write a macro and call the msgbox function to show it. I don't know ?!

5. Step by step
Note 1: Before compilation and execution, you must disable the kV Real-Time Virus monitoring function (Kv programs will interfere with our calls, but rising does not matter ).
NOTE 2: In the example program, no or few conditional judgments are used to represent the key part of the program. To make your program robust, add conditional judgment and Exception Handling on your own.

Step 1: how to start and disable word and the most basic usage of Variant
Step 2: similar to step 1, ccomvariant is used to improve the usage of variant.
Step 3: Create a Word document based on step 2, and transfer some characters from the program to the word
Step 4: Save the Word document on the basis of step 3.
Step 5: Use a small application to sort the input Chinese characters by strokes.
Step 6: A small application is used to steal the Word document in use.
The preceding six small programs have detailed annotations. After reading this article, you will experience and experiment with each other, so that you can freely manipulate any office.

Vi. Reference:Microsoft Office Development with Visual Studio
 

Related Article

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.