ms-officeatloffice-addinsoffice-2010comaddin

How to get IRM encryption status of an Office document in a COM Add-in?


I wrote an Office COM Add-in in C++ (ATL). Now I need to know in the Add-in if the current document is IRM (Information Rights Management) encrypted or unprotected. Alternatively, getting the currently applied IRM template would also work. Please note that IRM encryption is not the same as setting a document password. It's actually for Office 2010, but I would assume it's the same in newer versions. If you know the answer for .NET COM Add-ins, that might also help.


Solution

  • As nobody seems to know this, here's the answer I found: Microsoft Permission interface.

    In short: use the Permission interface. The property Enabled answers this. Actually the link shows the .NET Interop interface, but accessing this from C++ ATL is similar and much more direct (thus faster):

    First you need to store the reference to the application from the OnConnection call. Then you can get the required property like this (_app.ActiveDocument.Permission.Enabled):

    CComPtr<IDispatch> pApp=_app;
    CComVariant vDoc;
    pApp.GetPropertyByName(L"ActiveDocument", &vDoc);
    CComPtr<IDispatch> pDoc=vDoc.pdispVal;
    CComVariant vPermission;
    pDoc.GetPropertyByName(L"Permission", &vPermission);
    CComPtr<IDispatch> pPermission=vPermission.pdispVal;
    CComVariant vEnabled;
    pPermission.GetPropertyByName(L"Enabled", &vEnabled);
    fEnabled=vEnabled.boolVal!=VARIANT_FALSE;
    

    Error handling was omitted here for brevity. Make sure you handle return codes and check returned types etc.

    This was for Word. Excel would use ActiveWorkbook instead.