none
Cannot get hyperlink from winword document: TYPE MISMATCH RRS feed

  • Question

  • I have the following code in my COM Add-In for MS Office to go through all hyperlinks in winword document and then parse them:

    ...
    HRESULT hr;
    ULONG nRet;

    CComQIPtr<MSWORD::Hyperlinks> hylinks = doc->Hyperlinks; // CComQIPtr<_Document> doc
    ATLASSERT(hylinks);

    IEnumVARIANTPtr pEnum = hylinks->Get_NewEnum(); 

    for (int i = 1; i <= hylinks->Count; i++)
    {
        VARIANT v = {};
        hr = pEnum->Next(1, &v, &nRet);
        if (FAILED(hr)) continue;

        CComQIPtr<MSWORD::Hyperlink> link;
        ATLASSERT(link);

        hr = hylinks->raw_Item(&v, &link);
        if (FAILED(hr)) continue;
        ...
        // parse hyperlink's address
    }
    ...

    The result (hr) when raw_Item() is called, is 80020005 (DISP_E_TYPEMISMATCH)). And there is no trouble with code like this one when I go through Shapes.

    Does anyone can help me?

    Tuesday, September 1, 2020 6:08 PM

Answers

  • Finally, found out. How many collections have office application, so many ways to iterate them. Here we can do it like following, assign index of type VARIANT manually, not with enumerator:

    CComQIPtr<MSWORD::Hyperlinks> hylinks = doc->Hyperlinks;  // CComQIPtr<_Document> doc
    ATLASSERT(hylinks);

    for (int i = 1; i <= hylinks->Count; i++)
    {
        VARIANT ind;
        ind.iVal = i;
        ind.vt = VT_I2;

        CComQIPtr<MSWORD::Hyperlink> link;
        ATLASSERT(link);
        link = hylinks->Item(&ind);

        // do your magic
    }

    • Marked as answer by bru.net Tuesday, October 20, 2020 8:29 AM
    • Edited by bru.net Tuesday, October 20, 2020 8:33 AM
    Tuesday, October 20, 2020 8:29 AM