Cannot Change Protection With VBA In Word 2007

  • Follow


With Word 2007, I cannot use VBA to permanently change the type of
protection applied to a document.  VBA changes the protection while
the document is opened but once the document is closed the changes are
not written to the OOXML on disk.

Steps to reproduce:

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!
0
Reply Trevor 5/27/2010 8:41:01 PM

0 Replies
792 Views

(page loaded in 0.01 seconds)


Reply: