问题:您能给我介绍点用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);