1. Create a simple dialog box under VCProgramOpen the Class Wizard, click Add class-> from a library on the right, and find the word encapsulation class library, which is generally in C: /program files/Microsoft Office/office12 (my name is word2007), which contains msword8.olb or msword9.olb (my name is MSWord. olb), open, select the _ application, documents, _ document, and selection classes.
2. Add # include "MSWord. H", # include "atlbase. H" and # include "comdef. H" in the. h or. cpp file ". Then add it to the initinstance function of the program.
/* Initialize the COM component, which is MSWord */
If (! Afxoleinit ())
{
Afxmessagebox ("cocould not initialize COM services ");
Return false;
}
The preparation is complete.
The followingCodeWritten in the oninitdialog () function of the form. (Idc_richedit1 is cricheditctrl)
_ Application m_app; // word Application Object
Documents m_docs; // All document objects in Word
_ Document m_doc; // Word Document Object
Selection m_slection; // select an object for the document
Colevariant vtrue (short) True ),
Vfalse (short) False ),
Vopt (long) disp_e_paramnotfound, vt_error );
//******************************** * ***************************
try
{
If (! M_app.createdispatch (_ T ("word. application ") // start WORD
{< br> afxmessagebox (_ T (" check whether office is installed ");
return false;
}< br> m_app.setvisible (false);
m_docs = m_app.getdocuments ();
ccomvariant template (_ T (""));
ccomvariant newtemplate (false), documenttype (0), visible;
m_doc = m_docs.open (colevariant ("C: // remotecaptureguide.doc"), vfalse, vtrue, vfalse, vopt, vopt,
vopt, vopt ); // open an existing Word document
// m_doc = m_docs.add (& template, & newtemplate, & documenttype, & visible); // Add a new blank document
}
Catch (_ com_error & E)
{
MessageBox (E. errormessage ());
}
// Read text
M_slection = m_app.getselection (); // Word document cursor Input Point
M_slection.getsections (); // gets the optical punctuation.
M_slection.wholestory (); // select all the current documents
Setdlgitemtext (idc_richedit1, m_slection.gettext (); // set text content
// Destroy the object
M_slection.releasedispatch (); // release the selection object
M_docs.releasedispatch (); // release the documents object
M_doc.close (vopt, vopt, vopt );
M_doc.releasedispatch (); // release the Document Object
M_app.quit (vopt, vopt, vopt );
M_app.releasedispatch (); // release an application class object.
//************************************** ***************************
Success. The core code has been provided. If you have other debugging problems, you can solve them yourself (* ^__ ^ ....... Good luck.