1. 導入word文件
View ----ClassWizard-------Member Varibles----Add Class----From a type Library---C:\Program Files\Microsoft Office\Office12\MSWORD.OLB
2. 新建四個類
_Application
Documents
_Document
Range
3.
MFC中RichEdit控件為什么不顯示
RichEdit控件需要在程序中加入OLE初始化,不是拿來就能用的,具體可以查看MSDN,在InitInstance中先用AfxInitRichEdit進行初始化.
void CXSWordDlg::OnOK()


{
// TODO: Add extra validation here
CFileDialog file(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"All Files(*.*)|*.*| |",AfxGetMainWnd());
if(file.DoModal()==IDOK)

{
strText=file.GetPathName();
m_edit.SetWindowText(strText);
}
//CDialog::OnOK();
}

顯示按鈕事件
void CXSWordDlg::OnButxs()


{
// TODO: Add your control notification handler code here
//word應用程序
_Application app;
//初始化連接
app.CreateDispatch("word.Application");
Documents doc;
CComVariant a (_T(strText)),b(false),c(0),d(true);
_Document doc1;
doc.AttachDispatch( app.GetDocuments());
doc1.AttachDispatch(doc.Add(&a,&b,&c,&d));
Range range;
//求出文檔的所選區(qū)域
range = doc1.GetContent();//取出文件內(nèi)容
CString str;
str = range.GetText();
m_rich.SetWindowText(str);
//關閉
app.Quit(&b,&c,&c);
//釋放環(huán)境
app.ReleaseDispatch();
}
