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):
- 1=wdAllowOnlyComments,
- 2=wdAllowOnlyFormFields,
- 3=wdAllowOnlyReading,
- 0=wdAllowOnlyRevisions,
- -1=wdNoProtection.
- 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.
- 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.
- True if the specified section is protected for forms.
–jeroen
Filed under: Delphi, Delphi XE2, Delphi XE3, Delphi XE4, Development, Office, Office 2000, Office 2003, Office 2007, Office 2010, Office 2013, Office Automation, Power User, Software Development, Word