none
word interop changing Formatting Restriction within protect document RRS feed

  • Question

  • we have an Office Automation written in c# to include data in a specific Section. After we insert the data in the Word document we protect the section where we added the Data. To do this we use the following code =>

    WordEngine wemain =...
    ...            
    wemain.ProtectDocument(Microsoft.Office.Interop.Word.WdProtectionType.wdAllowOnlyFormFields, bFalse, szPassword, bFalse, bFalse);

    All this works fine.

    Problem now is that the Customer wants to be able to turn the Line Numbering on and off in the protected File. For this I saw that Microsoft offers Settings in the Restrict Editing Dialog => Formatting Restriction.

    Does anyone knows how I can change this via interop?

    Best Regards

    Michael



    MK

    Wednesday, November 2, 2016 7:17 PM

All replies

  • Hi MK,

    >> For this I saw that Microsoft offers Settings in the Restrict Editing Dialog => Formatting Restriction.

    Do you mean you want to set formatting restrictions for a projected document? If so, I think you need to unprotect it, then set it and protect the documents again.

    Here is a simple code in VBA:

      ActiveDocument.Unprotect Password:="123"
      ActiveDocument.Styles("Body Text").Locked = True
      ActiveDocument.Styles("Body Text 2").Locked = False
      ActiveDocument.Protect Password:="123", NoReset:=False, Type:= _
            wdNoProtection, UseIRM:=False, EnforceStyleLock:=True

    If you have any trouble to achieve these code by interop, please feel free to let us know.

    Best Regards,

    Edward


    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.

    Thursday, November 3, 2016 4:36 AM
  • Hi Edward,

    with the formatting restriction dialog you can define what a user can change after you have protected the document. In our case we won't to be able turn the Line Numbering on and off even when the Document is protected. I also wand to set the "Allow AutoFormatt to override formatting restriction"

    You can get to this Dialog when you click on Restrict Editing=>

    and in the Restrict Editing you have to click on Settings...

    Regards

    Michael


    MK



    Thursday, November 3, 2016 6:38 AM
  • Hi MK,

    You could not formatting restrictions while the document is protected. For formatting restrictions for protected documents, you need to unprotect it first, set formatting restrictions, and then protect it again.

    Best Regards,

    Edward


    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.

    Thursday, November 3, 2016 7:36 AM
  • Hi Edward

    thanks again for your answer. I know that I have to do this before I protect the Word document. but how can I Change this via interop?

    Regards

    Michael


    MK

    Thursday, November 3, 2016 8:37 AM
  • Hi MK,

    I suggest you try something like below:

                doc.Unprotect("123");
                doc.Styles["heading 1"].Locked = true;  //disable heading 1
                doc.Styles["heading 2"].Locked = false; //enable heading 2
                doc.Protect(WdProtectionType.wdNoProtection, Type.Missing, "123", Type.Missing, true);

    Best Regards,

    Edward


    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.

    Thursday, November 3, 2016 10:27 AM
  • Hi Edward

    as you can see in my first post I already do this.

    But as I mentioned I wand to uncheck the Line Number checkbox in the “Formatting Restrictions” Dialog via interop. And that piece is missing =>



    MK


    Thursday, November 3, 2016 10:35 AM
  • Hi MK,

    For Line Number, you could try

                doc.Unprotect("123");
                doc.Styles["Line Number"].Locked = !doc.Styles["Line Number"].Locked;//true;  //disable heading 1
                //doc.Styles["heading 2"].Locked = false; //enable heading 2
                doc.Protect(WdProtectionType.wdNoProtection, Type.Missing, "123", Type.Missing, true);

    Best Regards,

    Edward


    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.

    Friday, November 4, 2016 2:57 AM
  • How to default the edit restriction drop down to Filling in forms in C# or VB.net
    In the introp dll do we have any method to Have only free form editable in the protected word document?
    Tuesday, July 9, 2019 7:27 PM

  • How to default the edit restriction drop down to Filling in forms in C# or VB.net
    In the introp dll do we have any method to Have only free form editable in the protected word document?
    Tuesday, July 9, 2019 7:29 PM

  • How to default the edit restriction drop down to Filling in forms in C# or VB.net
    In the introp dll do we have any method to Have only free form editable in the protected word document?
    Tuesday, July 9, 2019 7:30 PM