用VC写word文件方法
2018-06-21 02:27:24来源:未知 阅读 ()
问题:您能给我介绍点用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.
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 制表位怎么设置?Word排版不用制表符,只不过是瞎忙一场 2020-07-09
- 流程图怎么做?用Word制作流程图超方便! 2020-07-07
- Word文档怎么让每一章设置不同页眉?书籍中惯用的排版技巧! 2020-07-05
- 如何让一个Word文档同时拥有纵向和横向页面? 2020-07-03
- Word公司专用文档模板这样做,一劳永逸! 2020-06-05
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash