同上

解决方案 »

  1.   

    _Application m_App; //定义Word提供的应用程序对象;
    Documents m_Docs; //定义Word提供的文档对象;
    Selection m_Sel; //定义Word提供的选择对象;
    _Document m_ActiveDoc; //定义word提供的活动对象
     
    m_Docs.ReleaseDispatch();
    m_Sel.ReleaseDispatch();
    m_App.ReleaseDispatch();
    m_App.m_bAutoRelease = true;

    if(!m_App.CreateDispatch("Word.Application"))

    AfxMessageBox("创建Word2000服务失败!"); 
    exit(1); 
    }
    strFilePath = strWholePathName+"word\\report.doc";

    //定义VARIANT变量;
    COleVariant varFilePath(strFilePath);
    COleVariant varstrNull("");
    COleVariant varZero((short)0);
    COleVariant varTrue(short(1),VT_BOOL);
    COleVariant varFalse(short(0),VT_BOOL); COleVariant vTrue((short)TRUE), vFalse((short)FALSE),
    vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    m_Docs.AttachDispatch(m_App.GetDocuments());//将Documents类对象m_Docs和Idispatch接口关联起来;
    m_Docs.Open(varFilePath,varFalse,varFalse,varFalse,varstrNull,
    varstrNull,varFalse,varstrNull,varstrNull,varTrue,varTrue,varTrue);
    //打开Word文档; 
    m_Sel.AttachDispatch(m_App.GetSelection());//将Selection类对象m_Sel和Idispatch接口关联起来;

    m_Sel.MoveDown(COleVariant(short(4)),COleVariant(short(4)),COleVariant(short(0)));
    for(i=0;i<m_list.GetCount();i++)
    {
    pos = m_list.FindIndex(i);

    m_Sel.MoveDown(COleVariant(short(4)),COleVariant(short(1)),COleVariant(short(0)));
    m_Sel.TypeText(itoa(i+1,buf,10));
    m_Sel.MoveDown(COleVariant(short(4)),COleVariant(short(1)),COleVariant(short(0)));
    m_Sel.TypeText(pMangData->strName);
    m_Sel.MoveDown(COleVariant(short(4)),COleVariant(short(1)),COleVariant(short(0)));
    m_Sel.TypeText(itoa(pMangData->nCount,buf,10));
    }
    //保存文件 

    m_ActiveDoc = m_App.GetActiveDocument(); 

    strSavePath=strWholePathName+"word\\1.doc";

    m_ActiveDoc.SaveAs(COleVariant(strSavePath), COleVariant((short)0), 
    vFalse, COleVariant(""), vTrue, COleVariant(""), vFalse, vFalse, vFalse, vFalse, vFalse); m_Docs.ReleaseDispatch();
    m_Sel.ReleaseDispatch(); //退出WORD 
    m_App.Quit(vOpt, vOpt, vOpt);
    m_App.ReleaseDispatch(); //打开文档

    strExePath=_T("D:\\Microsoft Office\\Office\\Winword.exe ");
    strExePath+=strWholePathName;
    strExePath+="word\\1.doc";

    WinExec(strExePath,SW_SHOW);
      

  2.   

    _Application app;
    app.CreateDispatch(_T("Word.Application"));
    app.SetVisible(TRUE); Documents docs=app.GetDocuments();
    CComVariant Template(_T("")); //不使用WORD的文档模板
    CComVariant NewTemplate(false),DocumentType(0),Visible;
    _Document doc = docs.Add(&Template,&NewTemplate,&DocumentType,&Visible);

           //绘制5*6的表格
    CComVariant vFalse(false),vTrue(true);
    Tables tables = doc.GetTables();
    _Table table = tables.Add(sel.GetRange(),5,6,&vTrue,&vFalse);
           //往表格填充文字
          ....................
           //释放资源
            table.ReleaseDispatch();
    tables.ReleaseDispatch();
    doc.ReleaseDispatch();
    docs.ReleaseDispatch(); CComVariant SaveChanges(false),OriginalFormat,RouteDocument;
    app.Quit(&SaveChanges,&OriginalFormat,&RouteDocument);
    app.ReleaseDispatch();
      

  3.   

    插入图片
    COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    COleVariant varTrue(short(1),VT_BOOL);
    COleVariant varFalse(short(0),VT_BOOL);
    InlineShapes shapes = doc.GetInlineShapes();
    _InlineShape shape = shapes.AddPicture("c:\\001.jpg",&varTrue,&varTrue,covOptional);