none
Cannot Change Protection With Automation In Word 2007

    Question

  • With Word 2007, I cannot use VBA, PIA, or VSTO Add-In to permanently change the type of
    protection applied to a document.  The protection can be changed while
    the document is opened but once the document is closed the changes are
    not written to the OOXML on disk.

    Steps to reproduce using VBA:

    Step 1
    Create a new document in Word 2007.

    Step 2
    From the VBA Immediate Window, execute the following statement.

    ActiveDocument.Protect Password:="MyPassword", NoReset:=True,
    Type:=wdAllowOnlyComments, UseIRM:=False, EnforceStyleLock:=False

    Step 3
    Verify that the specified protection has been added to the new
    document.

    ? ActiveDocument.ProtectionType = wdAllowOnlyComments
    True

    Step 4
    Save the document as "Test.docx" to a convenient location (i.e.
    Desktop) and close.

    Step 5
    Inspect the "Test.docx/word/settings.xml" document part using a tool
    such as "Open XML SDK 2.0 Productivity Tool for Microsoft Office".
    There should be an element like the following.

    <w:documentProtection w:edit="comments" w:enforcement="1"
    w:cryptProviderType="rsaFull" w:cryptAlgorithmClass="hash"
    w:cryptAlgorithmType="typeAny" w:cryptAlgorithmSid="4"
    w:cryptSpinCount="100000" w:hash="..." w:salt="..." xmlns:w="http://
    schemas.openxmlformats.org/wordprocessingml/2006/main" />

    Step 6
    Open "Test.docx" in Word 2007 and run the following in the VBA
    Immediate Window.

    ActiveDocument.Unprotect "MyPassword"
    ActiveDocument.Protect Password:="MyPassword", NoReset:=True,
    Type:=wdAllowOnlyRevisions, UseIRM:=False, EnforceStyleLock:=False

    Step 7
    Verify that the protection for "Test.docx" has been changed to the
    specified type.
    ? ActiveDocument.ProtectionType = wdAllowOnlyRevisions
    True

    Step 8
    Close "Test.docx" saving changes.

    Step 9
    Inspect the "Test.docx/word/settings.xml" document part again using
    the same method as step 5.  The documentProtection element will appear
    unchanged:

    <w:documentProtection w:edit="comments" w:enforcement="1"
    w:cryptProviderType="rsaFull" w:cryptAlgorithmClass="hash"
    w:cryptAlgorithmType="typeAny" w:cryptAlgorithmSid="4"
    w:cryptSpinCount="100000" w:hash="..." w:salt="..." xmlns:w="http://
    schemas.openxmlformats.org/wordprocessingml/2006/main" />

    According to ECMA 376 2nd Ed., after the VBA commands in step 6, it
    should be:

    <w:documentProtection w:edit="trackedChanges" w:enforcement="1"
    w:cryptProviderType="rsaFull" w:cryptAlgorithmClass="hash"
    w:cryptAlgorithmType="typeAny" w:cryptAlgorithmSid="4"
    w:cryptSpinCount="100000" w:hash="..." w:salt="..." xmlns:w="http://
    schemas.openxmlformats.org/wordprocessingml/2006/main" />

    There appears to be a disconnection between the Word document object
    model in memory and the OOXML written to disk.  Can anyone else
    reproduce this behavior?  If yes, any insights on how to resolve this
    will be very much appreciated!

    Thursday, June 03, 2010 12:19 AM

All replies

  • Hi Trevor

    Have you discussed this in a forum that specializes in OpenXML? There's OpenXMLDeveloper.org and the Open XML SDK forums...

    FWIW, Word is notorious for not saving changes if nothing in the document BODY has changed. It doesn't hold true for all things, but it does for some.

    have you tried doing any editing, then saving? Does that make a difference?


    Cindy Meister, VSTO/Word MVP
    Friday, June 04, 2010 3:48 PM
  • Editing does not make a difference.  I have a VBA add-in that a customer expects to allow them to have documents that should sometimes be protected for revisions and at other times protected for comments.  Changing the protection type programmatically in Word 2007 is broken.  I can use automation to change the protection and it will appear changed as long as the document remains open.  Once the document is closed however it reverts to whatever protection type was first applied.  This is true for every form of automation I have tried: VBA macros, IronPython/PIA, and VSTO Add-In.  Perhaps it is notewothy that if the protection type is changed by the user using the "Restrict Formatting and Editing" pane, it does persist?

    I have not posted this to any OOXML forum as the issue is really that the Word 2007 object model does not behave as documented.  I have simply inspected the OOXML to prove the errant behavior observed using VBA.

    Friday, June 04, 2010 5:24 PM
  • Hi Trevor,

     

    I have the same issue using Word 2007/2010 for docx/rtf documents. Extra issue is related the password is not set correct after unprotect and protect again the document using a different password.

    See this post: http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/f9b8408d-ed73-4e84-ac78-54526ceb5037

    Microsoft confirm this is an issue, but till now I haven’t an answer when will be fixed.

    Do you find a workaround for your problem ? (except using doc format, or save/close/open again the document)

     

    Greetings,

    Dan Boanta 

    Wednesday, April 27, 2011 6:50 AM