Cannot get hyperlink from winword document: TYPE MISMATCH

50 Views Asked by At

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.

Can anyone help me?

1

There are 1 best solutions below

0
On BEST ANSWER

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
}