Shapes.Range method: how to call from C++?


  • I need to write C++ code that group three shapes in document. I have three “smart” pointers to shapes (leftConnectorPtr, rightConnectorPtr and textBoxPtr) and I must “combine” them into the ShapeRange object.

    I use the Shapes.Range method and pass array with shapes names to that method.

    Here my code (sorry, my browser has failed -- I can not use forum "tags"):

    rgSABound.cElements = 3;
    rgSABound.lLbound = 1;
    SAFEARRAY* pSafeArray = SafeArrayCreateEx(VT_BSTR, 1, &rgSABound, NULL);
    if (pSafeArray)
        BSTR* pData;
        HRESULT hResult = SafeArrayAccessData(pSafeArray, reinterpret_cast<void**> (&pData));
        if (SUCCEEDED(hResult))
            pData[0] = SysAllocString(leftConnectorPtr->GetName());
            pData[1] = SysAllocString(rightConnectorPtr->GetName());
            pData[2] = SysAllocString(textBoxPtr->GetName());

            CComVariant rangeIndexes(pSafeArray);

            footerPtr->GetShapes()->Range(&rangeIndexes);    // fails!

            // group the shapes...

            HRESULT hResult = SafeArrayAccessData(pSafeArray, reinterpret_cast<void**> (&pData));
            if (SUCCEEDED(hResult))


    At the line, where the Range(…) method is called, my application fails with exception. What’s wrong?

    • Moved by Cindy Meister MVPMVP Tuesday, November 04, 2008 3:02 PM C++ questions are off-topic in the VSTO forum (Moved from Visual Studio Tools for Office to Off-Topic Posts (Do Not Post Here))
    Tuesday, November 04, 2008 1:54 PM

All replies

  •  C++ questions are off-topic in the VSTO forum. The Please Read First message at the top of the forum briefly describes what VSTO is and what is supported in the forum. It also contains links to venues for Office-related, non-VSTO questions. I suggest you try an office.developer newsgroup, or a newsgroup that specializes in C++.

    FWIW, no matter where you ask, you should provide information about the version of Word (I assume this is Word and not Excel or PowerPoint) and provide information about the other objects you use in your code. For example, I wonder what footerPtr represents...
    Cindy Meister, VSTO/Word MVP
    Tuesday, November 04, 2008 3:00 PM