题目:您能给我先容点用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(); 其函数实现以下:
- LPDISPATCH CEmbed_WordCntrItem::GetIDispatch()
- {
- ASSERT_VALID(this);
- ASSERT(m_lpObject != NULL);
- LPUNKNOWN lpUnk = m_lpObject;
- Run();
- LPOLELINK lpOleLink = NULL;
- if(m_lpObject->QueryInterface(IID_IOleLink,(LPVOID FAR*)&lpOleLink)== NOERROR)
- {
- ASSERT(lpOleLink != NULL);
- lpUnk = NULL;
- if(lpOleLink->GetBoundSource(&lpUnk) != NOERROR)
- {
- TRACE0("Warning: Link is not connected!\n");
- lpOleLink->Release();
- }
- ASSERT(lpUnk != NULL);
- }
- LPDISPATCH lpDispatch = NULL;
- if(lpUnk->QueryInterface(IID_IDispatch,(LPVOID FAR*)&lpDispatch) != NOERROR)
- {
- TRACE0("Waring: does not support IDispatch!\n");
- return NULL;
- }
- ASSERT(lpDispatch != NULL);
- return lpDispatch;
- }
经过过程此函数来返回标准COM接口IDispatch。
(5)在Embed_WordView.cpp中添加对"MSWord8.h"的援引:#include "MSWord8.h",如利用Word2000,则包含对"MSWord9.h"的援引。 然后在视类CEmbed_WordView中添加函数EmbedAutomateExcel()
- void CEmbed_WordView::EmbedAutomateWord()
- {
- BeginWaitCursor();
- CEmbed_WordCntrItem* pItem = NULL;
- TRY
- {
- CEmbed_WordDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- pItem = new CEmbed_WordCntrItem(pDoc);
- ASSERT_VALID(pItem);
- GetClientRect(&pItem->rect);
- CLSID clsid;
- 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);
- LPDISPATCH lpDisp;
- lpDisp = pItem->GetIDispatch();
- }
- CATCH(CException, e)
- {
- if (pItem != NULL)
- {
- ASSERT_VALID(pItem);
- pItem->Delete();
- }
- AfxMessageBox(IDP_FAILED_TO_CREATE);
- }
- END_CATCH
- EndWaitCursor();
- }
若是细心研究过这段代码,会发现它同AppWizard主动天生的OnInsertObject()函数有着惊人的类似程度,看一下View类中的 OnInsertObject() 编制,对此中的注释引发了我们的乐趣,由于它和我们刚写的编制有惊人的类似。事实上,我们刚才写的只不过是OnInsertObject()的一个特例:OnInsertObject()许可用户从可用的OLE对象列表当选择其一插入到利用法式中。由于在此我们只需对Word进行主动化,所以派生了这一行动。
(6)为了在法式刚启动时便将Word嵌入到法式中来,还需在视类的OnInitialUpdate()函数中添加代码:
- void CEmbed_WordView:nInitialUpdate()
- {
- CView:nInitialUpdate();
- EmbedAutomateWord(); //将Word嵌入
- m_pSelection = NULL;
- }
(7)为了使嵌入的工作区占满全部客户区可以经过过程点窜OnDraw函数来实现:
- void CEmbed_WordView:nDraw(CDC* pDC)
- {
- CEmbed_WordDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- if (m_pSelection == NULL)
- {
- POSITION pos = pDoc->GetStartPosition();
- m_pSelection = (CEmbed_WordCntrItem*)pDoc->GetNextClientItem(pos);
- }
- if (m_pSelection != NULL)
- {
- CRect rect;
- 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);
- LPUNKNOWN lpUnk = m_lpObject;
- Run();
- LPOLELINK lpOleLink = NULL;
- if (m_lpObject->QueryInterface(IID_IOleLink, (LPVOID FAR*)&lpOleLink) == NOERROR)
- {
- ASSERT(lpOleLink != NULL);
- lpUnk = NULL;
- if (lpOleLink->GetBoundSource(&lpUnk) != NOERROR)
- {
- TRACE0("Warning: Link is not connected!\n");
- lpOleLink->Release();
- return NULL;
- }
- ASSERT(lpUnk != NULL);
- }
- LPDISPATCH lpDispatch = NULL;
- if (lpUnk->QueryInterface(IID_IDispatch, (LPVOID FAR*)&lpDispatch)!=NOERROR)
- {
- TRACE0("Warning: does not support IDispatch!\n");
- return NULL;
- }
- ASSERT(lpDispatch != NULL);
- return lpDispatch;
(5)。在officeView.h添加#include "msword8.h" //若是是Word2000则为#include "msword9.h"
(6)。点窜void COfficeView:nInsertObject(),源码以下:
- BeginWaitCursor();
- COfficeCntrItem* pItem = NULL;
- TRY
- {
- // Create new item connected to this document.
- COfficeDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- pItem = new COfficeCntrItem(pDoc);
- ASSERT_VALID(pItem);
- // Initialize the item from the dialog data.
- /* if (!dlg.CreateItem(pItem))
- AfxThrowMemoryException(); // any exception will do
- ASSERT_VALID(pItem);*/
- CLSID clsid; //
- 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; // set selection to last inserted item
- pDoc->UpdateAllViews(NULL);
- // As an arbitrary user interface design, this sets the selection
- // to the last item inserted.
- // TODO: reimplement selection as appropriate for your application
- m_pSelection = pItem; // set selection to last inserted item
- pDoc->UpdateAllViews(NULL);
- }
- CATCH(CException, e)
- {
- if (pItem != NULL)
- {
- ASSERT_VALID(pItem);
- pItem->Delete();
- }
- AfxMessageBox(IDP_FAILED_TO_CREATE);
- }
- END_CATCH
- EndWaitCursor();
- (7)重载ID—FILE—SAVE,
- void COfficeView:nFileSave()
- {
- // TODO: Add your command handler code here
- TRY{
- LPDISPATCH lpDisp;
- lpDisp = m_pSelection->GetIDispatch();
- Documents docs;
- _Application app;
- _Document mydoc;
- Documents my;
- mydoc.AttachDispatch (lpDisp,TRUE);
- app=mydoc.GetApplication ();
- /* app.Run ("Macro3");*/
- mydoc.Activate ();
- BOOL password=mydoc.GetHasPassword ();
- mydoc.SetPassword ("love");
- password=mydoc.GetHasPassword ();
- COleVariant vFalse((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,click insert object,and edit ,and save.maybe run macro.
声明:欢迎各大网站转载本站文章,还请保留一条能直接指向本站的超级链接,谢谢!
时间:2012-06-13 10:43:56,点击:65824
【声明】:以上文章或资料除注明为Office自创或编辑整理外,均为各方收集或网友推荐所得。其中摘录的内容以共享、研究为目的,不存在任何商业考虑。如有任何异议,请与本站联系,本站确认后将立即撤下。谢谢您的支持与理解!
相关评论
我要评论
评论内容
相关文章
推荐文章