Note: In Office 2007, some class names are changed to followed by 0, such as crange0 cdocument0, some function names are also changed, and the format is changed to get_text (), for example, gettext (),
Read the Word document:
Remember to modify cdocuments, cdocument0
In the header file: // Add # pragma once
# Pragma
Once
# Import "K: \ Program Files XXX \ Microsoft Office \ Office XX \ MSWord. olb" no_namespace raw_interfaces_only \
Rename ("findtext", "_ findtext ")\
Rename ("rectangle", "_ rectangle ")\
Rename ("exitwindows", "_ exitwindows ")
As I said above, after importing typelib, add classes such as application document. The following is an snippet.
# Include "capplication. H"
# Include "cdocument0.h"
# Include "cdocuments. H"
Capplication oword;
Cdocuments odocs;
Cdocument0 odoc;
Void cvcreadwritertfdlg: onbnclickedbutton1 ()
{
// Variable Declaration
Colevariant vtrue (short) True ),
Vfalse (short) False ),
Vopt (long) disp_e_paramnotfound, vt_error );
Capplication owordapp;
Cselection Osel;
Cdocuments odocs;
Cdocument0 odoc;
Cparagraphs paragraphs;
Cstringarray arytext;
Long pacounst;
Int lC;
// Create a word Instance Object
If (! Owordapp. createdispatch (text ("word. application "))){
Afxmessagebox (text ("createdispatch failed."), mb_ OK & brvbarmb_setforeground );
Return; // be sure to return; otherwise, the program will crash.
}
// Display this object file. You can also not call this sentence or display the object file
Owordapp. set_visible (true );
// Create a document
Odocs = owordapp. get_documents ();
// Open a new document and add the content of E: \ buildjobedit. rtf to the new document.
Ccomvariant TPL (_ T ("E: \ buildjobedit. rtf"), newtemplate (false), doctype (0), Visble;
Odoc = odocs. Add (& TPL, & newtemplate, & doctype, & Visble );
// Select all content in the document
Osel = owordapp. get_selection ();
Osel. wholestory (); // This sentence must exist; otherwise, the subsequent code is invalid.
// Obtain all segments
Paragraphs = Osel. get_paragraphs ();
Pacounst = paragraphs. get_count ();
// Store each segment in the array
For (lc = 1; LC <pacounst + 1; LC ++ ){
// Get a paragraph
Cparagraph paragraph;
Paragraph = paragraphs. Item (long) Lc );
// Get a text segment
Crange range = paragraph. get_range ();
Cstring strrangetext = range. get_text ();
Cfont font = range. get_font ();
// Set the font size and color of the text
// Font. put_color (RGB (0,255, 0 ));
// Font. put_size (12 );
// Afxmessagebox (strrangetext );
// Save it to the array
Arytext. Add (strrangetext );
// Release the object
Paragraph. releasedispatch ();
}
Read the Excel Code:
1. Add L before the string, such as l "Fox", which is the reason for Unicode.
2. Some functions are different. It is the same as the first link, for example, getvalue. Now it should be written as get_value. This is the correct one under the author's role and there are some code errors, but at least the following code runs correctly.
1. Create an MFC project, MDI (based on the dialog box), and use other default values.
2. Excel file initialization:
A. Project-> Add class-> MFC class from typelib (Project-> Add class-> mfccategory in typelibimport to excel.exe, usually in C:/program files/Microsoft Office/workshop)
B. Select the following items: _ application, _ worksheet, _ workbook, worksheets, workbooks, and range, and then import them;
C. After the import, classes such as capplication, cworksheet, cworkbook, cworksheets, cworkbooks, and crange are automatically added to the project;
Then you need to delete the first sentence # import "...... excel. EXE" nonamespace in the header file of these classes;
If compilation encounters an error after the introduction, not enough actual parameters for macro 'dialogboxw'. This is a headache!
Solution: In the crange class, add the underline before dialogbox () to _ dialogbox!
Variant dialogbox ()
{
Variant result;
Invokehelper (0xf5, dispatch_method, vt_variant, (void *) & result, null );
Return result;
}
Void cexcel open 3dlg: onbnclickedok ()
{
// Todo: add the control notification handler code here
Capplication app;
Cworkbooks books;
Cworkbook;
Cworksheets sheets;
Cworksheet sheet;
Crange range;
Crange icell;
Lpdispatch lpdisp;
Colevariant vresult;
Colevariant covoptional (long) disp_e_paramnotfound, vt_error );
If (! App. createdispatch (L "Excel. application "))
{
Afxmessagebox (L "cannot start the Excel server! ");
Return;
}
Books. attachdispatch (App. get_workbooks ());
Lpdisp = books. open ("D: // test.xls", covoptional, kernel, covoptional, kernel, covoptional );
// Obtain the workbook
Book. attachdispatch (lpdisp );
// Obtain the worksheets
Sheets. attachdispatch (book. get_worksheets ());
// Obtain the currently active Sheet
// If a cell is being edited, this operation cannot be returned and will remain waiting.
Lpdisp = book. get_activesheet ();
Sheet. attachdispatch (lpdisp );
// Read the value of the first cell
Range. attachdispatch (sheet. get_cells ());
Range. attachdispatch (range. get_item (colevariant (long) 2), colevariant (long) 1). pdispval );
/* Colevariant */vresult = range. get_value2 ();
Cstring STR;
If (vresult. Vt = vt_bstr) // string
{
STR = vresult. bstrval;
}
Else if (vresult. Vt = vt_r8) // 8-byte number
{
Str. Format (L "% F", vresult. dblval );
}
/* Else if (vresult. Vt = vt_date) // time format
{
Systemtime st;
Varianttimetosystemtime (& vresult. Date, & St );
}
Else if (vresult. Vt = vt_empty) // empty cell
{
STR = "";
}*/
Books. Close ();
App. Quit (); // exit
// Release the object
Range. releasedispatch ();
Sheet. releasedispatch ();
Sheets. releasedispatch ();
Book. releasedispatch ();
Books. releasedispatch ();
App. releasedispatch ();
Onok ();
MessageBox (STR );
}
In addition, remember to add:
# Include "capplication. H"
# Include "crange. H"
# Include "cworkbook. H"
# Include "cworkbooks. H"
# Include "cworksheet. H"
# Include "cworksheets. H"