VC reads Word documents

Source: Internet
Author: User

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.

 

 

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.