The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,649 other followers

Some notes on Word automation and protected documents

Posted by jpluimers on 2014/08/26

Though I’ve done this automation in Delphi, this applies to automation from any development platform. In this particular project, I had to update Word documents. That is fine, unless your documents are protected. You can find out if a document is protected by probing the ProtectionType property of a Document object.

If a document is protected, and you try to change something you should not, you get an error message like this:

This method or property is not available because the document is a protected document.

If you have control of the documents or templates involved, then you can take the recommended steps from Document Handling with protected Microsoft Office Word Template:

Microsoft Office Word offers the possibility to enable document protection only to certain sections of a document. So you can place Bookmarks used by Dynamics AX in a section that is kept unprotected and the Form Controls in a section where Document Protection is enabled.

If you don’t, then you have to check for protection, unprotect, do the modifications, then re-protect the document.

If you are working on the ActiveDocument of a word application, then the property to check is ActiveDocument.ProtectionType.

Note there is no way to ask Word for the current protection password.

VBA code would look like this (adapted from Macro to Unlock a Locked Word 2007 Document and Use ActiveDocument.Unprotect only if document is protected):

Dim OriginalProtection As WdProtectionType
OriginalProtection = ActiveDocument.ProtectionType
If OriginalProtection <> wdNoProtection Then
  ActiveDocument.Unprotect
  ' If password protected, pass a password in the above call, as otherwise it can fail.
  MsgBox "I'm unlocked. Do your deed."
Else
  MsgBox "I wasn't locked to start with"
End If
' Perform the business logic on the word document
If OriginalProtection <> wdNoProtection Then
  ActiveDocument.Protect Type:=OriginalProtection, NoReset:=True
  ' If password protected, pass a password in the above call, as otherwise the document can be unprotected without password.
  MsgBox "I'm back in chains."
End If

Delphi code like this:

Below is a table with the relevant objects, properties and method documentation links for the various Office versions (documentation for Office XP and Office 2000 is not available any more):

Office 2013 release. Microsoft Office 2010. Microsoft Office 2007. Microsoft Office 2003. Microsoft Office XP. Microsoft Office 2000. Remarks
Document Object (Word). Document Object (Word). Document Object [Word 2007 Developer Reference]. Document Object.
Document.ProtectionType Property (Word). Document.ProtectionType Property (Word). ProtectionType Property [Word 2007 Developer Reference]. ProtectionType Property. value: readonly WdProtectionType

  •  1=wdAllowOnlyComments,
  •  2=wdAllowOnlyFormFields,
  •  3=wdAllowOnlyReading,
  •  0=wdAllowOnlyRevisions,
  • -1=wdNoProtection.
Document.Protect Method (Word). Document.Protect Property (Word). Protect Property [Word 2007 Developer Reference]. Protect Method. parameters:

  • type: WdProtectionType
  • NoReset Optional VARIANT False to reset form fields to their default values; True to retain the current form field values if the document is protected. If Type is not wdAllowOnlyFormFields, NoReset is ignored.
  • Password Optional VARIANT If supplied, the password to be able to edit the document, or to change or remove protection.
  • UseIRM Optional VARIANT Specifies whether to use Information Rights Management (IRM) when protecting the document from changes.
  • EnforceStyleLock Optional VARIANT Specifies whether formatting restrictions are enforced for a protected document.
Document.Unprotect Method (Word). Document.Unprotect Method (Word). Unprotect Method [Word 2007 Developer Reference]. UnProtect Method. parameters:

  • Password Optional Variant The password string used to protect the document. Passwords are case-sensitive. If the document is protected with a password and the correct password isn’t supplied, a dialog box prompts the user for the password.
Section Object (Word). Section Object (Word). Section Object [Word 2007 Developer Reference]. Section Object.
Section.ProtectedForForms Property (Word). Section.ProtectedForForms Property (Word). ProtectedForForms Property [Word 2007 Developer Reference]. ProtectedForForms Property. value: Read/write Boolean.

  • True if the specified section is protected for forms.

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: