none
使用COM智能指针 操作word 添加图形 总是创建图形对象失败?帮忙看下代码哪出问题了创建的是win32程序不是mfc的 RRS feed

  • 问题


  • #include "stdafx.h"

    #import "C:\\Program Files (x86)\\Microsoft Office\\Office12\\MSWORD.OLB" no_namespace raw_interfaces_only \
    rename("FindText","_FindText") \
    rename("Rectangle","_Rectangle") \
    rename("ExitWindows","_ExitWindows")
    #include <atlbase.h>
    #include <atlcom.h>

    #include <string>

    using namespace std;
    int _tmain(int argc, _TCHAR* argv[])
    {

    HRESULT hr;

    if (S_FALSE==::CoInitialize(NULL))
    {
    printf("初始化com失败");
    }

    _ApplicationPtr app=NULL;
    app.CreateInstance(__uuidof(Application));
    app->put_Visible(true);
    DocumentsPtr docs=NULL;
    app->get_Documents(&docs);
    _DocumentPtr doc=NULL;
    doc.CreateInstance(__uuidof(Document));
    CComVariant  Template(_T("D:\\JOB\\Projects\\Test\\wordDocTest\\6.docx"));
    CComVariant  NewTemplate(false),DocumentType,Visible;
    docs->Add(&Template,&NewTemplate,&DocumentType,&Visible,&doc);




    ShapesPtr shapes=NULL;
    doc->get_Shapes(&shapes);

    /*CComVariant Start(10),End(20);
    doc->Range(&Start,&End,&range1);*/

    ShapePtr shape=NULL;
    RangePtr p_Range=NULL;
    SelectionPtr sel=NULL;
    app->get_Selection(&sel);
    RangePtr firstRange;
    RangePtr secondRange;
    ParagraphsPtr pras;
    doc->get_Paragraphs(&pras);
    ParagraphPtr pra;
    pras->Item(1,&pra);
    pra->get_Range(&firstRange);


    CComVariant Anchor(firstRange);
        hr= shapes->AddLine(10,10,100,100,&Anchor,&shape);

    //问题就在这里 调用addline后shape仍为空   __vfptr = CXX0030: Error: expression cannot be evaluated  返回值hr = -2146824070;



    //hr=shapes->AddTextbox(MsoTextOrientation(1),0,30,25,25,&Anchor,&shape);

    }

         
    2013年4月2日 4:44

全部回复