站长资讯网
最全最丰富的资讯网站

VC写word 用VC写word文件方法

VC写word 用VC写word文件方法,我们在工作中会遇到很多问题,像VC写word这类问题应该大家也都遇到过吧,今天这篇VC写word 用VC写word文件方法的文章,专门为你解答了这个问题,相信你看完后一定会收获很多!

问题:您能给我介绍点用Visual C++ 动态生成WORD文档资料吗?给个例子代码什么的。解答:一:

以下是创建这个MFC应用程序的步骤:(1)使用AppWizard创建一个新的MFC AppWizard(EXE)工程,命名为"Embed_Word"(2)选择单文档视图(SDI)结构,在第3步中需要选中Container,以提供容器支持。 其它都为默认。在ClassView中将产生如下类应用类: CEmbed_WordApp in Embed_Word.h and Embed_Word.cpp框架类: CMainFrame in MainFrm.h and MainFrm.cpp文档类: CEmbed_WordDoc in Embed_WordDoc.h and Embed_WordDoc.cpp视图类: CEmbed_WordView in Embed_WordView.h and Embed_WordView.cpp容器类: CEmbed_WordCntrItem in CntrItem.h and CntrItem.cpp(3)在View菜单中,选ClassWizard,选Automation选项卡,选Add Class,选择From a TypeLibrary, 在Office目录中选中Microsoft Word 97/2000 类型库Word8.olb或Word9.olb,会将把类型库中的所有类添加到你的工程中。这时,ClassView中会多出几十个类,可以通过这些类提供的接口来实现必要的功能。(4)在CCntrItem.h中添加获取标准COM接口IDispach的函数: LPDISPATCH GetIDispatch(); 其函数实现如下:

LPDISPATCHCEmbed_WordCntrItem::GetIDispatch(){ASSERT_VALID(this);ASSERT(m_lpObject!=NULL);LPUNKNOWNlpUnk=m_lpObject;Run();LPOLELINKlpOleLink=NULL;if(m_lpObject->QueryInterface(IID_IOleLink,(LPVOIDFAR*)&lpOleLink)==NOERROR){ASSERT(lpOleLink!=NULL);lpUnk=NULL;if(lpOleLink->GetBoundSource(&lpUnk)!=NOERROR){TRACE0("Warning:Linkisnotconnected!n");lpOleLink->Release();}ASSERT(lpUnk!=NULL);}LPDISPATCHlpDispatch=NULL;if(lpUnk->QueryInterface(IID_IDispatch,(LPVOIDFAR*)&lpDispatch)!=NOERROR){TRACE0("Waring:doesnotsupportIDispatch!n");returnNULL;}ASSERT(lpDispatch!=NULL);returnlpDispatch;}

通过此函数来返回标准COM接口IDispatch。(5)在Embed_WordView.cpp中添加对"MSWord8.h"的引用:#include "MSWord8.h",如使用Word2000,则包含对"MSWord9.h"的引用。 然后在视类CEmbed_WordView中添加函数EmbedAutomateExcel()

voidCEmbed_WordView::EmbedAutomateWord(){BeginWaitCursor();CEmbed_WordCntrItem*pItem=NULL;TRY{CEmbed_WordDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);pItem=newCEmbed_WordCntrItem(pDoc);ASSERT_VALID(pItem);GetClientRect(&pItem->rect);CLSIDclsid;if(FAILED(::CLSIDFromProgID(L"Word.document",&clsid)))AfxThrowMemoryException();if(!pItem->CreateNewItem(clsid))AfxThrowMemoryException();ASSERT_VALID(pItem);pItem->DoVerb(OLEIVERB_SHOW,this);m_pSelection=pItem;pDoc->UpdateAllViews(NULL);LPDISPATCHlpDisp;lpDisp=pItem->GetIDispatch();}CATCH(CException,e){if(pItem!=NULL){ASSERT_VALID(pItem);pItem->Delete();}AfxMessageBox(IDP_FAILED_TO_CREATE);}END_CATCHEndWaitCursor();}

如果仔细研究过这段代码,会发现它同AppWizard自动生成的OnInsertObject()函数有着惊人的相似程度,看一下View类中的 OnInsertObject() 方法,对其中的注释引起了我们的兴趣,因为它和我们刚写的方法有惊人的相似。事实上,我们刚才写的只不过是OnInsertObject()的一个特例:OnInsertObject()允许用户从可用的OLE对象列表中选择其一插入到应用程序中。因为在此我们只需对Word进行自动化,所以派生了这一行为。(6)为了在程序刚启动时便将Word嵌入到程序中来,还需在视类的OnInitialUpdate()函数中添加代码:

voidCEmbed_WordView:nInitialUpdate(){CView:nInitialUpdate();EmbedAutomateWord();//将Word嵌入m_pSelection=NULL;}

(7)为了使嵌入的工作区占满整个客户区可以通过修改OnDraw函数来实现:

voidCEmbed_WordView:nDraw(CDC*pDC){CEmbed_WordDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);if(m_pSelection==NULL){POSITIONpos=pDoc->GetStartPosition();m_pSelection=(CEmbed_WordCntrItem*)pDoc->GetNextClientItem(pos);}if(m_pSelection!=NULL){CRectrect;GetClientRect(&m_pSelection->rect);m_pSelection->OnGetItemPosition(rect);m_pSelection->Draw(pDC,rect);}}

二:在VC中调用WORD(显示,修改,存盘,运行宏)(1)使用AppWizard创建一个新的MFC AppWizard(EXE)工程,命名为"office"(2)选择单文档视图(SDI)结构,在第3步中需要选中Container,以提供容器支持,并且选中active document container 其它都为默认(3)在View菜单中,选ClassWizard,选Automation选项卡,选Add Class,选择From a TypeLibrary, 在Office目录中选中Microsoft Word 97/2000 类型库Word8.olb或Word9.olb,选中application,document,_document。单击ok(4)给COfficeCntrItem添加一Public方法。LPDISPATCH GetIDispatch()其源码如下:

ASSERT_VALID(this); ASSERT(m_lpObject!=NULL); LPUNKNOWNlpUnk=m_lpObject; Run(); LPOLELINKlpOleLink=NULL; if(m_lpObject->QueryInterface(IID_IOleLink,(LPVOIDFAR*)&lpOleLink)==NOERROR) { ASSERT(lpOleLink!=NULL); lpUnk=NULL; if(lpOleLink->GetBoundSource(&lpUnk)!=NOERROR) { TRACE0("Warning:Linkisnotconnected!n"); lpOleLink->Release(); returnNULL; } ASSERT(lpUnk!=NULL); } LPDISPATCHlpDispatch=NULL; if(lpUnk->QueryInterface(IID_IDispatch,(LPVOIDFAR*)&lpDispatch)!=NOERROR) { TRACE0("Warning:doesnotsupportIDispatch!n"); returnNULL;} ASSERT(lpDispatch!=NULL); returnlpDispatch;

(5)。在officeView.h添加#include "msword8.h" //如果是Word2000则为#include "msword9.h"(6)。修改void COfficeView:nInsertObject(),源码如下:

BeginWaitCursor();COfficeCntrItem*pItem=NULL; TRY { //Createnewitemconnectedtothisdocument. COfficeDoc*pDoc=GetDocument(); ASSERT_VALID(pDoc); pItem=newCOfficeCntrItem(pDoc); ASSERT_VALID(pItem);//Initializetheitemfromthedialogdata. /*if(!dlg.CreateItem(pItem)) AfxThrowMemoryException();//anyexceptionwilldo ASSERT_VALID(pItem);*/ CLSIDclsid;// if(FAILED(::CLSIDFromProgID(L"Word.document",&clsid))) AfxThrowMemoryException();if(bOpenStatus)//如果是打开文档 { if(!pItem->CreateFromFile(sFilename,clsid))//打开已有文档 AfxThrowMemoryException(); } else//否则新建文档 { if(!pItem->CreateNewItem(clsid))//新建文档 AfxThrowMemoryException(); } ASSERT_VALID(pItem);pItem->Activate(OLEIVERB_SHOW,this);ASSERT_VALID(pItem); m_pSelection=pItem;//setselectiontolastinserteditem pDoc->UpdateAllViews(NULL); //Asanarbitraryuserinterfacedesign,thissetstheselection //tothelastiteminserted.//TODO:reimplementselectionasappropriateforyourapplicationm_pSelection=pItem;//setselectiontolastinserteditem pDoc->UpdateAllViews(NULL); } CATCH(CException,e) { if(pItem!=NULL) { ASSERT_VALID(pItem); pItem->Delete(); } AfxMessageBox(IDP_FAILED_TO_CREATE); } END_CATCHEndWaitCursor();(7)重载ID—FILE—SAVE,voidCOfficeView:nFileSave() { //TODO:Addyourcommandhandlercodehere TRY{ LPDISPATCHlpDisp;lpDisp=m_pSelection->GetIDispatch();Documentsdocs;_Applicationapp;_Documentmydoc; Documentsmy;mydoc.AttachDispatch(lpDisp,TRUE); app=mydoc.GetApplication(); /*app.Run("Macro3");*/ mydoc.Activate(); BOOLpassword=mydoc.GetHasPassword(); mydoc.SetPassword("love"); password=mydoc.GetHasPassword(); COleVariantvFalse((short)FALSE); mydoc.SaveAs(COleVariant(filename),vFalse,vFalse,COleVariant(""),vFalse,//filename为一个以字符串表达的文件名如"c:\love1.doc" COleVariant(""),vFalse,vFalse,vFalse,vFalse,vFalse);} CATCH(CException,e) { } END_CATCH }bulid,clickinsertobject,andedit,andsave.mayberunmacro.

赞(0)
分享到: 更多 (0)
网站地图   沪ICP备18035694号-2    沪公网安备31011702889846号