I haven't been studying c ++ for a long time. I went to the forum yesterday and encountered such a question. I found the relevant content on the Internet.
My experience is summarized.
First, create a win32 console program. Be sure to select the support for MFC option.
Select View> Class Wizard, click Add Class, select Import from Type Library, and open MSWORD. OLB in the installation directory of Office. The following dialog box is displayed:
Import four classes: _ Application, Documents, _ Document, and Range.
Add the following code to the else statement of the main function.
_ Application wordApp; // the top-level object in the Object Inheritance structure. You can use the method to access other work-type objects (in this example, word)
Documents docs; // indicates all open document objects.
_ Document doc;
Range aRange;
COleVariant vTrue (short) TRUE ),
VFalse (short) FALSE ),
VOpt (long) DISP_E_PARAMNOTFOUND, VT_ERROR );
WordApp. CreateDispatch ("Word. Application", NULL );
WordApp. SetVisible (FALSE );
Docs = wordApp. GetDocuments ();
Doc = docs. open (COleVariant (filename ...), vFalse, vTrue, vFalse, vOpt, vOpt );
ARange = doc. Range (vOpt, vOpt );
String str (aRange. GetText ());
Cout <str <endl;
Doc. Close (vOpt, vOpt, vOpt );
WordApp. Quit (vOpt, vOpt, vOpt );
CoUninitialize (); // close the COM Object Library and release resources.