cannot get Document.SensitivityLabel property of AIP protected document

38 Views Asked by At

According to https://learn.microsoft.com/en-us/office/vba/api/word.document.sensitivitylabel, i can get Sensitivity Label from word document protected by AIP by using word object model.

I wrote a MFC application add the following code in button event:

HRESULT OLEMethod2(int nType, VARIANT* pvResult, IDispatch* pDisp, LPOLESTR ptName, int cArgs...)
{
    if (!pDisp) return E_FAIL;

    va_list marker;
    va_start(marker, cArgs);

    DISPPARAMS dp = { NULL, NULL, 0, 0 };
    DISPID dispidNamed = DISPID_PROPERTYPUT;
    DISPID dispID;

    // Get DISPID for name passed...
    HRESULT hr = pDisp->GetIDsOfNames(IID_NULL, &ptName, 1,
        LOCALE_USER_DEFAULT, &dispID);
    if (FAILED(hr)) {
        return hr;
    }
    // Allocate memory for arguments...
    VARIANT* pArgs = new VARIANT[cArgs + 1];
    // Extract arguments...
    for (int i = 0; i < cArgs; i++) {
        pArgs[i] = va_arg(marker, VARIANT);
    }

    // Build DISPPARAMS
    dp.cArgs = cArgs;
    dp.rgvarg = pArgs;

    // Handle special-case for property-puts!
    if (nType & DISPATCH_PROPERTYPUT) {
        dp.cNamedArgs = 1;
        dp.rgdispidNamedArgs = &dispidNamed;
    }

    // Make the call!
    hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT,
        nType, &dp, pvResult, NULL, NULL);
    if (FAILED(hr)) {
        return hr;
    }

    // End variable-argument section...
    va_end(marker);
    delete[] pArgs;
    return hr;
}
void CWordAutomateDlg::OnBnClickedButton2()
{
    ::CoInitialize(NULL);
    //Get Application ClassID
    CLSID clsid;
    HRESULT hr = CLSIDFromProgID(L"Word.Application", &clsid);
    if (FAILED(hr))
    {
        return;
    }

    //Create Application
    IDispatch* pWApp = NULL;
    hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void**)&pWApp);
    if (FAILED(hr))
    {
        return;
    }
    
    //Make application visible
    VARIANT x;
    x.vt = VT_I4;
    x.lVal = 1;
    hr = OLEMethod2(DISPATCH_PROPERTYPUT, NULL, pWApp, (LPOLESTR)L"Visible", 1, x);
    if (FAILED(hr))
    {
        return;
    }

    // GetDocuments
    IDispatch* pDocuments = NULL;
    {
        VARIANT result;
        VariantInit(&result);
        hr = OLEMethod2(DISPATCH_PROPERTYGET, &result, pWApp, (LPOLESTR)L"Documents", 0);
        if (FAILED(hr))
        {
            return;
        }
        pDocuments = result.pdispVal;
    }
    // OpenDocument
    IDispatch* activeDocument = NULL;
    {
        COleVariant vFname(L"d:\\test.docx");
        VARIANT fname = vFname.Detach();

        VARIANT result;
        VariantInit(&result);
        hr = OLEMethod2(DISPATCH_METHOD, &result, pDocuments, (LPOLESTR)L"Open", 1, fname);
        if (FAILED(hr))
        {
            return;
        }
        activeDocument = result.pdispVal;
    }
    //Get SensitivityLabel
    IDispatch* label = NULL;
    {
        VARIANT result;
        VariantInit(&result);
        hr = OLEMethod2(DISPATCH_PROPERTYGET, &result, activeDocument, (LPOLESTR)L"SensitivityLabel", 0);
        if (FAILED(hr))
        {
            TRACE(L"hr=%x", hr);
            return;
        }
    }
}

I did get the "Application" interface, the "Documents" interface, and let MS Word open and show an AIP protected document and get the "ActiveDocument" interface successfully. When try to get "SensitivityLabel" property from "ActiveDocument", i get an error 0x80020009 which means "Exception occurred".

I also tried to get SensitivityLabel in Office Addin and got the same result.

My computer is Window 10 pro 22H2, 19045.3930, my office version is 2108(Build 14326.20454 Click-to-Run)

I guess "SensitivityLabel" property may have not been implemented or there is some restriction to the application start the automation?

Anyone have the same problem or get the right "SensitivityLabel" please tell me, any suggestion will be helpful.

Thanks.

0

There are 0 best solutions below