码迷,mamicode.com
首页 > 其他好文 > 详细

使用msword.olb操作word(2)

时间:2015-05-27 12:17:13      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:合并   word   bookmark   

摘要:使用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();




使用msword.olb操作word(2)

标签:合并   word   bookmark   

原文地址:http://blog.csdn.net/weikangc/article/details/46044335

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!