摘要:使用msowrd.olb实现将另一个doc文件中的内容,赋值到当前打开的word文件中,并使用现当前文件的格式设置。也就是实现两个doc文件内容合并。
例子:
_Application oWordApp; Documents oWordDocs; _Document oWordDoc; _Document oWordDoc_; Selection oWordSel; Selection oWordSel_; Bookmarks oWordBkmks; Bookmark oWordBkmk; COleVariant vTrue((short)TRUE),vFalse((short)FALSE),vOpt((long)DISP_E_PARAMNOTFOUND,VT_ERROR); if (!oWordApp.CreateDispatch(_T("Word.Application"))) { AfxMessageBox(_T("Word CreateDispatch Failed!")); return ; } try { oWordApp.SetVisible(TRUE); oWordApp.Activate(); oWordDocs = oWordApp.GetDocuments(); CString strDoc_= _T("C:\\正文.doc"); oWordDoc_ = oWordDocs.Open(COleVariant(strDoc_),vFalse,vFalse,vFalse,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt); oWordSel_ = oWordApp.GetSelection(); oWordSel_.WholeStory(); oWordSel_.Copy(); oWordSel_.ReleaseDispatch(); oWordDoc_.Close(vOpt,vOpt,vOpt); oWordDoc_.ReleaseDispatch(); CString sField = _T("发文标题"); CString strDoc = _T("C:\\红头.doc"); oWordDoc = oWordDocs.Open(COleVariant(strDoc),vFalse,vFalse,vFalse,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt); oWordSel = oWordApp.GetSelection(); oWordSel.GoTo(COleVariant((short)-1),vOpt,vOpt,COleVariant(sField)); oWordDoc = oWordApp.GetActiveDocument(); oWordBkmks = oWordDoc.GetBookmarks(); oWordBkmks.SetDefaultSorting(-1); oWordBkmks.SetShowHidden(FALSE); oWordDoc.Save(); oWordSel.PasteAndFormat(0); oWordDoc.Save(); oWordBkmks.ReleaseDispatch(); oWordSel.ReleaseDispatch(); oWordDoc.Close(vOpt,vOpt,vOpt); oWordDoc.ReleaseDispatch(); oWordDocs.ReleaseDispatch(); } catch(...){} oWordApp.Quit(vOpt,vOpt,vOpt); oWordApp.ReleaseDispatch();
原文地址:http://blog.csdn.net/weikangc/article/details/46044335