How does this API work? RRS feed

  • Question

  • LPDISPATCH Shapes::AddPicture(LPCTSTR FileName, VARIANT* LinkToFile, VARIANT* SaveWithDocument, VARIANT* Left, VARIANT* Top, VARIANT* Width, VARIANT* Height, VARIANT* Anchor);

    I use it this way, no reaction.

            /*Word::*/_Application app = NULL;
    /*Word::*/Selection    sel = NULL;
    /*Word::*/Documents docs;
    /*Word::*/_Document doc;

    if (FAILED(::GetActiveObject(clsid, NULL, &pUnknown)))
    return FALSE;
    if (FAILED(pUnknown->QueryInterface(IID_IDispatch, (LPVOID *)&app)))
    throw(_T("No IDispatchPtr"));

    pUnknown = NULL;
    sel = app.GetSelection();

    if (!sel)
    throw(_T("No Edited Document"));
    catch(_com_error lpErr)
    if(pUnknown != NULL)

    return FALSE;

            doc = app.GetActiveDocument();
    Shapes  shapes = doc.GetShapes();

    float fTopPic = 0.0, fLeftPic = 0.0, fBottomPic = 50, fRightPic = 100;
    char fileName[MAX_PATH];
    GetModuleFileName(NULL, fileName, MAX_PATH);
    char dir[260];
    char dirver[100];
    _splitpath(fileName, dirver, dir, NULL, NULL);
    CString strAppPath = dirver;
    strAppPath += dir;
    VARIANT varTmp;
    VARIANT varTop,varLeft,varBottom,varRight;

    varTop.vt = VT_R4;;
    varTop.llVal = fTopPic;

    varLeft.vt = VT_R4;;
    varLeft.llVal = fLeftPic;

    varBottom.vt = VT_R4;;
    varBottom.llVal = fBottomPic;

    varRight.vt = VT_R4;;
    varRight.llVal = fRightPic;


    COleVariant covZero((short)0),
    covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR), 
    Shapes test = doc.GetShapes();
    CString str = strAppPath + _T("\\11.png");
    //COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    test.AddPicture(str, covTrue, covTrue, &varTop, &varLeft, &varBottom, &varRight, covOptional);

    Thursday, April 19, 2018 6:34 AM

All replies

  • SAFEARRAY *coords;
    VARIANT vcoords;
    SAFEARRAYBOUND saBound[2];
    saBound[0].lLbound = 0;
    saBound[0].cElements = 2;
    saBound[1].lLbound = 0;
    saBound[1].cElements = 3;
    coords = ::SafeArrayCreate(VT_R4, 2, saBound);
    if (coords)
    void HUGEP *pvData;
    ::SafeArrayAccessData(coords, (void **)&pvData);
    float point[2][3];
    point[0][0] = 50;
    point[0][1] = 50;
    point[0][2] = 200;

    point[1][0] = 250;
    point[1][1] = 260;
    point[1][2] = 290;

    //point[1][1] = 200;
    //point[1][0] = 200;
    //point[1][1] = 200;

    //point[][0] = 300;
    //point[][1] = 300;
    memcpy(pvData, point, 2 * 3 * sizeof(float));
    //V_VT(&vcoords);// = VT_R4 | VT_SAFEARRAY | VT_ARRAY;
    //V_VT(&vcoords) = VT_DISPATCH;//VT_ARRAY;//VT_SAFEARRAY; //| VT_ARRAY | VT_UI1;
    //V_VT(&vcoords) = VT_UI1 | VT_ARRAY;

    V_ARRAY(&vcoords) = coords;

    The experiment has been repeated many times.

    Wednesday, April 18, 2018 1:32 AM
  • Hi 油炸的兔子,

    From the description of the thread , It looks like the issue is more related with VC++ Forum.

    This forum only handles Office VBA Object Model related issues.

    So for better response and better solution for your issue, I move this thread to VC++ forum.

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us.

    Thank you for your understanding.



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 19, 2018 6:30 AM
  • This actually seems to be a question about how to use the CanvasShapes object exposed by the Microsoft Word Object Model.  Perhaps the Word for Developers forum would be a good spot to post this question.

    I don't know if Word's CanvasShapes object requires a particular type of data to be contained in the SAFEARRAY.  You might consider using long integers instead of floats.

    Also, it appears that the vcoords.vt member is never set.  For floats use V_VT(&vcoords) = VT_R4 | VT_ARRAY

    • Edited by RLWA32 Thursday, April 19, 2018 12:00 PM
    Thursday, April 19, 2018 11:58 AM
  • The question "How does this API work" followed by uncommented code is too vague to answer.

    If you have specific questions or errors have occurred then you should provide details of the errors and the related code that caused them

    It also seems that you are posting questions about how to use objects exposed by the Microsoft Word Object model.  You should consider posting your questions in the Word for Developers forum.

    Thursday, April 19, 2018 12:08 PM