none
HRESULT E_FAIL when accessing Document.Permission property RRS feed

  • Question

  • I'm trying to get permissions from a Word document in VSTO add-in:

    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Application.DocumentBeforeSave += ApplicationOnDocumentBeforeSave;
            Application.DocumentOpen += ApplicationOnDocumentOpen;
        }
    
        private void ApplicationOnDocumentOpen(Word.Document doc)
        {
            var p = doc.Permission;
        }
    
        private void ApplicationOnDocumentBeforeSave(Word.Document doc, ref bool saveAsUI, ref bool cancel)
        {
            var p = doc.Permission;
        }
    
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { }
    
        // Designer code omitted
    }

    But when doc.Permission is accessed (no matter which event handler), an error occurs:

    System.Runtime.InteropServices.COMException (0x80004005): 
        Error HRESULT E_FAIL has been returned from a call to a COM component.
    at Microsoft.Office.Interop.Word.DocumentClass.get_Permission()

    From the Permission interface documentation, it raises an error when the Windows Rights Management client is not installed. But I have Rights Management Service Client 2.1 installed. I tried different Office accounts, start with elevated permissions, run on a different computer, but the error is always the same.

    I'm using Visual Studio 2015 and Word 2016.


    Monday, May 22, 2017 3:06 PM

All replies

  • Hi AlekseyShubin,

    I try to test your code on my side.

    first I try to set the permission using code below.

    Sub demo()
    Dim objUserPerm As Office.UserPermission
     Set objUserPerm = ActiveDocument.Permission.Add( _
     "ABC@XYZ.com", _
     msoPermissionRead + msoPermissionEdit, #12/31/2017#)
     MsgBox "Permissions added for " & _
     objUserPerm.UserId, _
     vbInformation + vbOKOnly, _
     "Permissions Added"
     Set objUserPerm = Nothing
    
    
    End Sub

    then I try to debug your code.

    and I find that I did not receive any error and I can see the details regarding permission when I hover the mouse on variable "P".

    Reference:

    Permission.Add Method (Office)

    Document.Permission Property (Word)

    you can try to test it on your side and check the results.

    I try to check on my machine and I find that my machine does not have Windows Rights Management client installed.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, May 23, 2017 3:07 AM
    Moderator