誰有vc6.0匯入word2003生成的那兩個檔案,msword。h。cpp,能共享一下嗎
uj5u.com熱心網友回復:
僅供參考://1.在VC中新建一控制臺程式,選支持MFC(當然,你也可以不選擇支持MFC的,不過會很麻煩)
//2.按CTRL+W調出MFC ClassWizard,Add Class->From a type library,選擇你的word的型別庫
// (例如我的是word2003,安裝在e盤,我的路徑是"e:\edittools\microsoft office\office11\msword.olb"),
// 選擇完畢后,在彈出的視窗中選擇要讓classwizard生成的包裝類,在本例中要用到
// _Application,
// Documents,
// _Document,
// Range
// 這四個類,選中他們后按OK
//3.進入你的main函式所在的cpp檔案,加入頭檔案參考
// #include "msword.h" //參考剛才classwizard生成的idispatch包裝類
//4.加入代碼
// 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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/68548.html
上一篇:求教windows管理權限的問題
