如何vc 为Word添加矩形框并设置其属性用Shapes类的 AddShape()方法AddShape(long Type, float Left, float Top, float Width, float Height, VARIANT* Anchor)这个到底怎麽用啊?

解决方案 »

  1.   

    你可以在 word 里面录制宏,然后手动添加一个矩形并设置属性,最后参考一下宏里面的代码
      

  2.   

    Sub Macro1()
    '
    ' Macro1 Macro
    ' 宏在 2009-7-15 由 USER 录制
    '
        ActiveDocument.Shapes.AddShape msoShapeRectangle, 50, 50, 100, 200
        
    End Sub
    上述代码就是我录制的宏
    在msword.h中我找到了Shapes类及其中的AddShape(long Type, float Left, float Top, float Width, float Height, VARIANT* Anchor)方法我不清楚在VBA中 msoShapeRectangle这个参数与VC中的Long Type 是怎样对应的
      

  3.   

    添加图元可以先选中Range,然后执行Shapes的AddShape方法设置参数为线型即可,下例是设置图片  
      void   CWebOfficeCtrl::LoadUnit(void)  
      {  
      AFX_MANAGE_STATE(AfxGetStaticModuleState());  
       
      //   TODO:   在此添加调度处理程序代码  
      //m_pWebOfficeFrame->m_pWebOfficeView->m_pSelection->DeactivateUI();  
      LPDISPATCH   lpDisp;  
      lpDisp=m_pWebOfficeFrame->m_pWebOfficeView->m_pSelection->GetIDispatch();  
      //lpDisp=m_pWebOfficeFrame->m_pWebOfficeView->lpDisp;  
      _Application_Word   m_WordApp;  
      _Document_Word   m_WordDoc;  
      m_WordDoc.AttachDispatch(lpDisp,TRUE);  
      //m_WordDoc.Activate();  
      m_WordApp=m_WordDoc.GetApplication();  
       
      InlineShapes_Word   m_WordInlineShapes;  
      InlineShape_Word   m_WordInlineShape;  
      Selection_Word   m_WordSelection;  
      m_WordSelection=m_WordApp.GetSelection();  
      m_WordInlineShapes=m_WordSelection.GetInlineShapes();  
      COleVariant   vTrue((short)TRUE),vFalse((short)FALSE),vOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);  
      //AfxMessageBox(m_UnitName,MB_ICONINFORMATION);  
      m_WordInlineShape=m_WordInlineShapes.AddPicture(DownUnit(m_UnitName),vFalse,vTrue,vOptional);  
       
      //2003.11.29   Add   By   DigitalTitan[设置图元重叠属性]  
      Shape_Word   m_WordShape;  
      m_WordShape=m_WordInlineShape.ConvertToShape();  
      Shapes_Word   m_WordShapes;  
      VARIANT   varResult;  
      varResult.vt=VT_I4;  
      varResult.lVal=m_WordShapes.GetCount();  
      m_WordShape=m_WordShapes.Item(&varResult);    
      m_WordShape.Select(&varResult);  
       
       
      WrapFormat_Word   m_WordWrapFormat;  
      m_WordWrapFormat=m_WordShape.GetWrapFormat();  
      m_WordShape.SetRelativeHorizontalPosition(2);  
      m_WordShape.SetRelativeVerticalPosition(1);  
      if(m_UnitTop!="UnitTop")  
      {m_WordShape.SetTop((float)atoi(m_UnitTop));}  
      if(m_UnitLeft!="UnitLeft")  
      {m_WordShape.SetLeft((float)atoi(m_UnitLeft));}  
      if(m_UnitWidth!="UnitWidth")  
      {m_WordShape.SetWidth((float)atoi(m_UnitWidth));}  
      if(m_UnitHeight!="UnitHeight")  
      {m_WordShape.SetHeight((float)atoi(m_UnitHeight));}  
      m_WordWrapFormat.SetAllowOverlap(TRUE);  
      m_WordWrapFormat.SetType(3);  
      m_WordWrapFormat.ReleaseDispatch();  
      m_WordShape.ReleaseDispatch();  
      m_WordShapes.ReleaseDispatch();  
      //  
       
       
       
      m_WordInlineShape.ReleaseDispatch();  
      m_WordInlineShapes.ReleaseDispatch();  
      m_WordSelection.ReleaseDispatch();  
      m_WordDoc.ReleaseDispatch();  
      m_WordApp.ReleaseDispatch();  
      }
      

  4.   

    你用
    Office::msoShapeRectangle试一下
      

  5.   


    你说的加 Office是指的加Office命名空间吗?我是试了一下
    首先在StdAfx.h文件中加入
    #import "c:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE11\\mso.dll" rename_namespace("Office") named_guids,exclude("Pages")
    using namespace Office;可是这添加后就引发了更多的错误,我就更晕了。
      

  6.   

    #import "c:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE11\\mso.dll" rename_namespace("Office") named_guids,exclude("Pages") 
    你把 rename_namespace 去掉
    Office 是它默认的命名空间
    再试一下
    我这边用 Office::msoShapeRectangle 是可以编译通过的
      

  7.   

    我按照你这么写了,可是我这边怎么报'Shapes' : ambiguous symbol 歧义错误啊
    你那边可以再Word中创建一个矩形了吗?下边是我写的部分代码
    不知道错在哪了?请指正?

    COleVariant vTrue((short)TRUE),vFalse((short)FALSE),vOpt((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
    _Application app; if (!app.CreateDispatch("Word.Application",NULL))
    {
    AfxMessageBox("CreateDispatch failed.",MB_OK|MB_SETFOREGROUND);
    return ;
    }

    app.SetVisible(TRUE);
    app.Activate();

    Documents docs;
    _Document _doc;
    Selection sel;
    Tables theTables;
    Range wordRange;
    Table curTable;
    CComVariant defaultBehavior(1),AutoFitBehavior(1);
    Paragraphs   oParagraphs;
    Shapes shapes;
    VARIANT *anchor; Row oRow; docs=app.GetDocuments();//获得文档对象
    _doc=docs.Add(vOpt,vOpt,vOpt,vOpt); sel=app.GetSelection();
    sel.TypeText("one");
    oParagraphs=sel.GetParagraphs();
    oParagraphs.SetAlignment(1);//1=wdAlignParagraphCenter sel.TypeParagraph();//相当于换行
    sel.TypeText("two");

    oParagraphs=sel.GetParagraphs();
    oParagraphs.SetAlignment(2);//2=wdAlignParagraphRight sel.TypeParagraph(); sel.TypeText("three"); oParagraphs=sel.GetParagraphs();
    oParagraphs.SetAlignment(3);//2=wdAlignParagraphLeft
    _Document activedoc; activedoc=app.GetActiveDocument();

    //shapes=activedoc.GetShapes(); //shapes=sel.GetShapeRange(); shapes.AddShape(Office::msoShapeRectangle,50,50,50,30,anchor);

    theTables=activedoc.GetTables();

    wordRange=sel.GetRange();

    curTable=theTables.Add(wordRange,7,11,&defaultBehavior,&AutoFitBehavior); curTable=theTables.Item(1);//获得表格集合的第一个表格

    sel.TypeText(_T("Test1"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test2"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test3"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test4"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test5"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test6"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test7"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test8"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test9"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test10"));
    sel.MoveRight(COleVariant((short)1),COleVariant(short(1)),COleVariant(short(0)));
    sel.TypeText(_T("Test11"));



    int i=0,j=0;
    for(i=2;i<7;i+=2)
    {
    Cell c1=curTable.Cell(i,1);
    Cell c2=curTable.Cell(i+1,1);
    c1.Merge(c2);
    c1.ReleaseDispatch();
    c2.ReleaseDispatch();
    }

    activedoc.SaveAs(COleVariant("c:\\doc1.doc"),COleVariant((short)0),vFalse,COleVariant(""),vTrue,COleVariant(""),vFalse,vFalse,vFalse,vFalse,vFalse,vOpt,vOpt,vOpt,vOpt,vOpt);
      

  8.   

    我这边已经可以了,我的 Word 版本和你的不一样,所以代码有些不一样
    1.不要加 using namespace office
    2. msoShapeRectangle 要加上 Office::msoShapeRectangle
    #import   "C:\Program Files\Common Files\Microsoft Shared\OFFICE12\MSO.DLL"   
    #import   "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"   
    #import   "D:\Microsoft Office\Office12\msword.olb"  rename("FindText","_FindText"), rename("ExitWindows","_ExitWindows")   
    using namespace Word;// 不要加 using namespace Office;void Test( void )
    {
    AfxOleInit();
    //////////////////////////////////////////////////////////////////////////
    COleVariant vTrue((short)TRUE),vFalse((short)FALSE),vOpt((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
    _ApplicationPtr app; app.CreateInstance("Word.Application");

    app->ShowMe();
    // app->Activate(); Word::DocumentsPtr spDocs;
    Word::_DocumentPtr _spdoc;
    Word::SelectionPtr spsel;
    TablesPtr theTables;
    RangePtr wordRange;
    TablePtr curTable;    
    CComVariant defaultBehavior(1),AutoFitBehavior(1);
    ParagraphsPtr   oParagraphs;
    ShapesPtr shapes;
    VARIANT *anchor; RowPtr oRow; spDocs = app->GetDocuments();//获得文档对象
    _spdoc = spDocs->Add(vOpt,vOpt,vOpt,vOpt); spsel = app->GetSelection();
    spsel->TypeText("one");
    oParagraphs=spsel->GetParagraphs();
    // oParagraphs->put_Alignment(1);//1=wdAlignParagraphCenter spsel->TypeParagraph();//相当于换行
    spsel->TypeText("two"); activedoc = app->GetActiveDocument(); shapes=activedoc->GetShapes(); //shapes= spsel->GetShapeRange(); shapes->AddShape(Office::msoShapeRectangle,50,50,50,30);  // 这里指定 Office namespaceactivedoc->SaveAs(COleVariant(_T("c:\\doc1.doc")),COleVariant((short)0),vFalse,COleVariant(_T("")),vTrue,COleVariant(_T("")),vFalse,vFalse,vFalse,vFalse,vFalse,vOpt,vOpt,vOpt,vOpt,vOpt);}
      

  9.   

     非常感谢你!
        
    activedoc = app->GetActiveDocument();shapes=activedoc->GetShapes();
    上述代码中activedoc这个对象在哪定义的啊?
      

  10.   

    _DocumentPtr activedoc;activedoc = app->GetActiveDocument();    shapes=activedoc->GetShapes();    //shapes= spsel->GetShapeRange();    shapes->AddShape(Office::msoShapeRectangle,50,50,50,30);  // 这里指定 Office namespaceactivedoc->SaveAs(COleVariant(_T("c:\\doc1.doc")),COleVariant((short)0),vFalse,COleVariant(_T("")),vTrue,COleVariant(_T("")),vFalse,vFalse,vFalse,vFalse,vFalse,vOpt,vOpt,vOpt,vOpt,vOpt);
      

  11.   

    谁有操作WORD的封装类,拿出来共享一下吧,我有EXCEL的,不知道各位有没有兴趣
      

  12.   

    加上定义了,编译也通了,运行出对话框了,可是当我点击按钮测试的时候,就报Runtime Error!This application has requested the runtime to terminate it in an unusual way. Please contact the appliation's support team for more information.这个错可能是什么原因引起的,
    我的环境是 Xp 系统; VC6.0sp6 ;word2003.
    你有时间吗可以帮我看看嘛
    QQ:357127720
    msn:[email protected]
    谢谢!!!!!!
      

  13.   

    我刚刚学习,能提供给我一份吗?
    谢谢!
    QQ:357127720 
    msn:[email protected] 
    非常愿意和你成为朋友!
      

  14.   

    你可以打个断点跟一下,看是哪一句出了问题。
    或者把代码发给我 realplayerassist#sina.com