Search code examples
c#ms-wordms-officeopenxmlopenxml-sdk

How to set Microsoft Word document section or paragraph to read only using DocumentFormat.OpenXml


Is there a way to set an ms word document section/paragraph to read only programmatically? I am aware of restricting to read only in word as described by the below link. https://support.office.com/en-us/article/Restrict-or-permit-formatting-changes-69ca7857-db43-4785-9e14-7735db6d79bb

However I need to do it in C# because I need to check first if the user is in SharePoint group.


Solution

  • Yes, it is possible.

    First extend your document:

    Document extendedDocument = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
    

    Then Protect it:

    extendedDocument.Protect(Word.WdProtectionType.wdAllowOnlyReading, true, "password", true);
    

    Edit: see more about working with an IRM to more securely protect your documents

    Edit: see more about how to use the protect method: