64,282
社区成员
发帖
与我相关
我的任务
分享
// 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
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
printf(_T("Fatal Error: MFC initialization failed!\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
if (CoInitialize(NULL) != S_OK)
{
AfxMessageBox("初始化COM支持库失败!");
return -1;
}
_Application wordApp;
Documents docs;
_Document doc;
Range aRange;
COleVariant vTrue((short)TRUE), vFalse((short)FALSE), vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CString txt;
wordApp.CreateDispatch("Word.Application",NULL);
wordApp.SetVisible(FALSE);
docs=wordApp.GetDocuments();
doc=docs.Open(COleVariant("c:\\new\\测试.doc"),vFalse,vTrue,vFalse,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);
aRange=doc.Range(vOpt,vOpt);
txt=aRange.GetText();
AfxMessageBox(txt);//这里GetText得到的就是word文件的纯文本了,你可以将其写到txt文件中
printf("[%s]\n",txt.GetBuffer(txt.GetLength()));//里面的换行不是\r\n而是\r,所以需要输出重定向到文本文件看结果。
aRange.ReleaseDispatch();
doc.Close(vOpt,vOpt,vOpt);
doc.ReleaseDispatch();
docs.ReleaseDispatch();
wordApp.Quit(vOpt,vOpt,vOpt);
wordApp.ReleaseDispatch();
CoUninitialize();
}
return nRetCode;
}