none
Programatically change Word 2010 document Protection type RRS feed

  • Question

  • We would like to enforce and remove different types of
    Document Protection (Restrict Editing) on a whole Word 2010 document in code.

    Our code has worked in previous version of Word but is no
    longer working with the new version. When changing protection type from
    designed to unprotect the document and then protect it with a new protection type as follows.

    ProtectioType = ActiveDocument.ProtectionType 

    If ProtectioType = wdAllowOnlyRevisions Then

       ActiveDocument.Unprotect() 

       ActiveDocument.Protect(Password:="xxxxx",
          NoReset:=True, Type:= wdAllowOnlyFormFields, UseIRM:=False,
          EnforceStyleLock:=False)

    End If

    After ActiveDocument.Unprotect executed, ActiveDocument.ProtectionType becomes -1 which is correct. But in the document

    the Restrict Editing pane (From the document ribbon menu
    Review -> Restrict Editing) settings are not changed. The "2. Allow only this type of editing in the
    document:" checkbox is still checked for "Tacked changes". Therefore, when code ActiveDocument.Protect is executed,
    wdAllowOnlyFormFields protection type is not applied and it keeps wdAllowOnlyRevisions. However, after unprotecting the document in code and before
    calling the protect method in code, the "2. Allow only this type of editing in the document:" checkbox is still checked (in the Review->Restrict

    Editing panel in the ribbon menu of the document). As the
    result, the document is not protected as we want it to be when using the code.

    Are there any VBA/Word functions we can use to
    programmatically clear the settings in the Restrict Editing pane? Many
    thanks! Zengfu


    Thursday, February 23, 2012 7:27 AM

Answers

  • Hi Zengfu,

    I did a bit of testing with your code. I opened up Word and the VBA window so that I could see both as I stepped through the code. If I leave the If block in the code, the code to unprotect and reprotect the document never gets run unless the document was already set to wdAllowOnlyRevisions.

    If I remove the If block, the document always gets correctly restricted to allow only form field entry. If I step through the code line by line in VBA, I am able to see the "Restrict Formatting and Editing" pane change.

    Sub ChangeDocumentProtection()
      ActiveDocument.Unprotect
      ActiveDocument.Protect Password:="xxxx", NoReset:=False, Type:=wdAllowOnlyFormFields, UseIRM:=False, EnforceStyleLock:=False
    End Sub

    You could try adding a the following line just after you unprotect the document.  It will explicitly remove restrictions.

    ActiveDocument.Protect Type:=wdNoProtection

    Please let me know if any of this helps, and vote for it as helpful and mark it as an answer if it does.

    Thanks,

    Mike Corkery, MCT, MCITP, MCPD, MCSD, MCAD, MCDBA, MCP, MSF, etc.

    • Marked as answer by Bruce Song Thursday, March 8, 2012 9:54 AM
    Sunday, February 26, 2012 5:19 PM