CodePudding user response:
Fyi://1. Create a new console in the VC program, choose support MFC (of course, you can also choose not to support of MFC, but will be very troublesome)
//2. Press CTRL + W v MFC ClassWizard, Add a Class - & gt; From a type library, choose your word's type library
//(for example my word2003, installed in e disk, my path is "e: \ edittools \ Microsoft office \ office11 \ msword olb"),
//choice to end, in the pop-up window, select the let classwizard generated wrapper classes, in this case need
//_Application,
//Documents,
//_Document,
//Range
//the four classes, selected after they click OK
//3. Enter your main function of the CPP file, add a header file reference
//# include "msword. H"//reference just classwizard generated idispatch wrapper class
//4. Add the code
//console_word. CPP: Defines the entry point for the console application.
//
# include "stdafx. H"
# include "console_word. H"
# include "msword. H"
# ifdef _DEBUG
# define new DEBUG_NEW
# undef THIS_FILE
The static char THIS_FILE []=__FILE__;
# endif
/////////////////////////////////////////////////////////////////////////////
//The one and only The application object
CWinApp theApp;
Int _tmain (int arg c, TCHAR * argv [], TCHAR * envp [])
{
Int nRetCode=0;
//initialize the MFC and the print and the error on failure
if (! AfxWinInit (: : GetModuleHandle (NULL), NULL, : : GetCommandLine (), 0))
{
//TODO: change the error code to suit your needs
Printf (_T (" Fatal Error: MFC initialization failed! \ n "));
NRetCode=1;
}
The else
{
//TODO: code your application 's behaviors here.
If (CoInitialize (NULL).=S_OK)
{
AfxMessageBox (" initialize COM support library failed!" );
The return - 1;
}
_Application wordApp;
The Documents docs.
_Document doc.
The Range aRange;
COleVariant vTrue (TRUE) (short), vFalse ((short) FALSE), vOpt ((long) DISP_E_PARAMNOTFOUND VT_ERROR);
Cstrings TXT.
WordApp. CreateDispatch (" Word. Application ", NULL);
WordApp. SetVisible (FALSE);
Docs=wordApp. GetDocuments ();
Doc=docs. Open (COleVariant (" c: \ \ new \ \ test. Doc "), vFalse, vTrue, vFalse, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
ARange=doc. Range (vOpt vOpt);
TXT=aRange the GetText ();
AfxMessageBox (TXT);//for GetText here is plain text word file, you can write in the TXT file
Printf ("] [% s \ n ", TXT. GetBuffer (. TXT GetLength ()));//the newline instead of \ r \ n \ r, so you need to redirect output to a text file as a result,
ARange. ReleaseDispatch ();
Doc. Close (vOpt vOpt, vOpt);
Doc. ReleaseDispatch ();
Docs. ReleaseDispatch ();
WordApp. Quit (vOpt vOpt, vOpt);
WordApp. ReleaseDispatch ();
CoUninitialize ();
}
Return nRetCode;
}
CodePudding user response:
In the answer you ask plates, using vbCodePudding user response:
VBA itself does not support the generated exe executable program, can consider to use VB to realize