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
|
||
Document.Protect Method (Word). | Document.Protect Property (Word). | Protect Property [Word 2007 Developer Reference]. | Protect Method. | parameters:
|
||
Document.Unprotect Method (Word). | Document.Unprotect Method (Word). | Unprotect Method [Word 2007 Developer Reference]. | UnProtect Method. | parameters:
|
||
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.
|
–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
