网上查询了很多文章,最后可以总结成,由于Word对象需要用OLE容易,最好的方法就是用视图/文档的模式,因为向导就能帮你创建很多线程的代码,下面是处理简例,具体功能还需研究添加。

(使用VC6+Office2003)/(使用VC6+Office2010)

1.用向导创建MFC应用程序.

2.选择单或多文档,并在复合文档类型中选择container(容器) ,并勾选Active Document container

3.添加关于word的引用-打开ClassWinszrd的AddClass的from a typelibrary. 选择C:\Program Files\MicrosoftOffice\OFFICE11下的msword.olb(为方便,把所有对象都选上)

PS:Office2010是E:\Program Files\Microsoft Office\Office14\msword.olb,E盘为office安装目录,

4.修改CntrItem.h文件中类声明的继承父类,由默认的COleClientItem 改为COleDocObjectItem

5.为CxxxCntrItem添加下面共有成员函数

public:
    LPDISPATCH GetIDispatch();

实现如下:

LPDISPATCH CxxxCntrItem::GetIDispatch()
{
    
    //The this and m_lpObject pointers must be valid for this function
    //to work correctly. The m_lpObject is the IUnknown pointer to
    // this object.
    ASSERT_VALID(this);
    ASSERT(m_lpObject != NULL);
    LPUNKNOWN lpUnk = m_lpObject;
    //The embedded application must be running in order for the rest
    //of the function to work.
    Run();
    //QI for the IOleLink interface of m_lpObject.
    LPOLELINK lpOleLink = NULL;
    if (m_lpObject->QueryInterface(IID_IOleLink,(LPVOID FAR*)&lpOleLink) == NOERROR){
        ASSERT(lpOleLink != NULL);
        lpUnk = NULL;
        //Retrieve the IUnknown interface to the linked application.
        if(lpOleLink->GetBoundSource(&lpUnk) != NOERROR){
            TRACE0("Warning: Link is not connected!\n");
            lpOleLink->Release();
            return NULL;
        }
        ASSERT(lpUnk != NULL);
    }
    //QI for the IDispatch interface of the linked application.
    LPDISPATCH lpDispatch = NULL;
    if(lpUnk->QueryInterface(IID_IDispatch, (LPVOID FAR*)&lpDispatch)!=NOERROR){
        TRACE0("Warning: does not support IDispatch!\n");
        return NULL;
    }
    //After you verify that it is valid, return the IDispatch
    //interface to the caller.
    ASSERT(lpDispatch != NULL);
    return lpDispatch;
    
}

6.修改xxxView.cpp文件中函数OnInitialUpdate()为

void CxxxView::OnInitialUpdate()

{

CView::OnInitialUpdate();

// TODO: remove this code whenfinal selection model code is written

m_pSelection =NULL;   // initialize selection

EmbedAutomateWord();

}

7.为CxxxView.cpp增加下面公有函数

public:
    void EmbedAutomateWord();

实现如下:

void CEmbed_WordView::EmbedAutomateWord()
{
    //Change the cursor so that theuser knows that something exciting is going
    //on.
    BeginWaitCursor();
    CEmbed_WordCntrItem*pItem = NULL;
    TRY{
        //Get thedocument that is associated with this view, and be sure that itis
        //valid.
        CEmbed_WordDoc* pDoc =GetDocument();
        ASSERT_VALID(pDoc);
        //Create a newitem associated with this document, and be sure that it is
        //valid.
        pItem = new CEmbed_WordCntrItem(pDoc);
        ASSERT_VALID(pItem);
        // Get the ClassID for the Word document.
        // This is usedin creation.
        CLSID clsid;
        if(FAILED(::CLSIDFromProgID(L"Word.document",&clsid)))
            //Anyexception will do. You just need to break out of the
            //TRYstatement.
            AfxThrowMemoryException();
        // Create theWord embedded item.
        if(!pItem->CreateNewItem(clsid))
            //Anyexception will do. You just need to break out of the
            //TRYstatement.
            AfxThrowMemoryException();
        //Make sure thatthe new CContainerItem is valid.
        ASSERT_VALID(pItem);
        // Start theserver to edit the item.
        pItem->DoVerb(OLEIVERB_SHOW,this);
        // As anarbitrary user interface design, this sets the
        // selection tothe last item inserted.
        m_pSelection =pItem;   // Setselection to the last inserted item.
        pDoc->UpdateAllViews(NULL);
        //Query for thedispatch pointer for the embedded object. In
        //this case, thisis the Word document.
        //LPDISPATCH lpDisp;
        //lpDisp = pItem->GetIDispatch();
        //Add text to theembedded Word document.
        //  CDocumentwdDoc;
        //  CRangewdRange;
        //set CDocument0wdDoc to use lpDisp, the IDispatch* of the
        //actualdocument.
        //  wdDoc.AttachDispatch(lpDisp);
        //Get a CRangeobject for the document.
        //wdRange =wdDoc.Range(COleVariant( (long)DISP_E_PARAMNOTFOUND, VT_ERROR),
        //       COleVariant( (long)DISP_E_PARAMNOTFOUND, VT_ERROR ) );
        //Fill the rangewith the string "Hello, World!"
        //wdRange.put_Text("Hello, World!" );
        //wdRang.
    }
    //Clean up if something went wrong.
    CATCH(CException, e){
        if(pItem !=NULL){
            ASSERT_VALID(pItem);
            pItem->Delete();
        }
        AfxMessageBox(IDP_FAILED_TO_CREATE);
    }
    END_CATCH
        //Set the cursor back to normal so theuser knows exciting stuff
        //is no longer happening.
        EndWaitCursor();
    
}

8.编译运行.

转自:http://blog.sina.com.cn/s/blog_4b3c1f950100ifzl.html

ps:上面只是一个简单的讲述,对msword.olb的操作均未涉及,下篇文章再说使用单文档视图结构把Word嵌入到VC程序中(转) - dragoo747450 - 我的博客

使用单文档视图结构把Word嵌入到VC程序中(转)相关推荐

  1. C++--深入分析MFC文档视图结构(项目实践)

    1 必备基础知识概述 1.1 MFC 文档视图结构程序结构总揽 当我们使用 MFC AppWizard 生成一个 MFC 程序,选用所有默认的设置(当然也是 Multiple Documents ,本 ...

  2. 深入分析MFC文档视图结构(项目实践)

    文档视图结构(Document/View Architecture)是MFC的精髓,也是Observer模式的具体实现框架之一,Document/View Architecture通过将数据和其表示分 ...

  3. MFC之文档/视图结构应用程序

    文档/视图结构应用程序 一.文档/视图结构分析 MFC 通过多个类提供了对程序框架的支持,使用这些类可以简单地实现文档/视图结构:其中主要包括以下五个类: 应用程序类(CWinAPP):是 MFC 程 ...

  4. MFC让文档/视图结构程序支持滚动条

    MFC让文档/视图结构程序支持滚动条 2009-06-30 09:26要通过滚动条显示文档,还必须知道文档滚动到了什么位置:一旦用户拖动滚动条时要告诉视图改变在文档中 的位置.所有这些,由MFC的CS ...

  5. 文档视图结构下多视图间的控制

    程序结构: 创建了一个基于CFormView的MFC文档视图结构的程序,并用CSplitterWnd将窗口分割为2列,左边显示默认创建CFormView视图,右边是CHtmlView视图. 在CFor ...

  6. 视图切换—多模板文档视图结构的应用

    转自:http://www.winu.cn/htmls/208/065/ 一.概述 ①在一个MDI程序中,需要使用到不同类型的子窗口,而每种类型窗口有可能有很多个,对应不同的数据. 这时,可以使用多模 ...

  7. MFC文档/视图结构体系及SDI回顾(2)

    1.牢骚发在最前面 三件事情杂糅到了一起弄得我彻夜未眠. 香港大学的Offer不小心丢在了垃圾邮箱里,却阴差阳错的来了清华.平台是一方面,当时谈好的港府奖学金也就此泡汤,这笔钱是我急需的. 惊闻噩耗, ...

  8. OpenCV+MFC文档视图结构 构建简易PS图像处理软件

    简介 本文介绍了基于OpenCV图像处理库的MFC框架下的图像处理程序构建方法. 本项目依赖OpenCV库4.1.0版本,构建和编译由VS2017完成. 程序目录 simPS2.h/simPS2.cp ...

  9. MFC 单文档 视图类中CMyDoc* GetDocument() const编译时错误:缺少“;”(在“*”的前面)...

    转:https://blog.csdn.net/hanjieson/article/details/8194337 造成原因:在其他的类之中使用到MyView.h的头文件 解决方案 :在加MyView ...

  10. MFC 文档 视图 框架窗口间的关系 和消息传送规律

    在MFC中引入了文档-视结构的概念,文档相当于数据容器,视相当于查看数据的窗口或是和数据发生交互的窗口.因此一个完整的应用一般由四个类组成:CWinApp应用类,CFrameWnd窗口框架类,CDoc ...

最新文章

  1. IndexError: list index out of range的解决办法
  2. 微信之父张小龙:产品经理的必备书单(转)
  3. mybatis的typeAliases别名
  4. 一体机硬盘被格式化了的资料恢复法子
  5. 生物(一)ctDNA突变检测应用于肿瘤早期筛查
  6. Android PackageManagerService(三)pm命令安装流程详解
  7. xlsxwriter去掉网格线_(原创)xlsxwriter,python excel 写入数据\图表等操作_图表操作(二)...
  8. java多线程的15种锁
  9. 从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。
  10. arcgis建筑数据的矢量化(一)
  11. 腾讯文档如何设置保护单元格与工作表?
  12. 宏碁4750 Ubuntu16.04下安装NVIDIA 390显卡
  13. 文件传服务器上全是乱码,解决txt文件上传oss服务器乱码的问题
  14. python分析股票数据 彤_Python数据分析:股票数据
  15. android-percent-support-lib-sample
  16. 7-5 谷歌的招聘 (15 分)
  17. python输入一组数据找出被七除余一的数_C程序设计实验-1-7.doc
  18. MacOS好用的系统清理工具CleanMyMac有哪些特点功能?
  19. C# Spire简单实现导出word(去水印)
  20. 计算机网络实验:实验一 交换机基本配置

热门文章

  1. Boost Log : Trivial logging
  2. 实习僧-产品体验报告
  3. 你好Linux!第一篇——Linux的前世今生和应用
  4. 洛谷P1725 琪露诺 题解
  5. 最新彩虹Ds发卡源码模板-Cool模板源码
  6. Android 手机开机密码破解锁定
  7. 信号隔离器的功能原理是什么?
  8. 元旦贺卡html,元旦新年贺卡怎么做
  9. 讯飞语音测评SDK的搭建与运用(Android studio)
  10. (由Active Desktop保存在桌面引起)桌面图标蓝底