|
|
|
 |
How to change the behaviour of Word's document protection, so users formfields don't get reset when they unprotect and reprotect
|
Article contributed by Ibby and Geoff Whitfield
One of the biggest source of complaints from users about Word's forms
protection feature (accessed from Tools + Protect or Unprotect document) is that
when you unprotect and reprotect a form, all the form fields are reset and the
data is lost.
This is a double-bind because of the fact that so many Word features are pointlessly
disabled, even when only a part of a document is protected for forms. For more
details, see: How to enable the spellchecker in a protected document.
Because of the second problem, users frequently do want to unprotect and reprotect
documents, which leaves them with the form fields resetting problem.
Note that in Word 2002 (Office XP), the problem does not arise if you
reprotect the document using the Protect Form button of the Forms toolbar:
The form fields only get reset in Word 2002 if you use the menu. But in Word
97 and 2000, the form fields are reset whichever you use.
You can fix this by intercepting
the built-in ToolsProtectUnprotectDocument and ProtectForm
commands. Just paste the following code into an Addin.
(Or you can paste it into a template, but if you use an addin, the problem
will be fixed regardless of which template is in use.) Remove the horizontal
line separating the two Subs, below, after pasting the
code in. If your Addin is only for Word 2002, you won't require the ProtectForm
macro, but will still require the ToolsProtectUnprotectDocument
one.
Sub
ToolsProtectUnprotectDocument()
Dim oDoc As Document
Set oDoc = ActiveDocument
On Error GoTo ErrMess
If oDoc.ProtectionType = wdNoProtection Then
With
Dialogs(wdDialogToolsProtectDocument)
.noreset = True
.Show
End With
Else
oDoc.Unprotect
End If
Exit Sub
ErrMess:
MsgBox Err.Description, vbInformation
End Sub
Sub ProtectForm()
If
ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect
Else
ActiveDocument.Protect
Type:=wdAllowOnlyFormFields, noreset:=True
End If
End Sub
Note: If you do occasionally want the formfields to reset, you
can just select Edit + Select All (or press Ctrl+A), while the document
is unprotected, and press F9 (Update Fields).
Or in Word 2002, there is an additional button on the Forms toolbar to reset
the form fields (while protection is switched off), without updating any other
fields an eraser. You could, if you wanted to, create a similar button
for Word 97/2000 and assign it to the
following macro: Sub
ResetFmFlds()
Dim FmFld As FormField
If ActiveDocument.ProtectionType = wdNoProtection Then
For Each
FmFld In ActiveDocument.FormFields
FmFld.Range.Fields(1).Update
Next
FmFld
End If
End Sub
Also note that if the user updates the fields (by Selecting All and pressing
F9) in an unprotected document then the form fields will get
reset; this is by design. And therefore the form fields will also get reset if
the user prints an unprotected document, and if Update fields is checked under Tools + Options
+ Print. This behaviour is by design on the basis that only a forms
designer ought to be updating the fields of, or printing, an unprotected
document containing form fields; and for a forms designer, the ability to reset
the form fields by updating them is sometimes very useful. However, if you
wanted to change this behaviour, you could either intercept
the UpdateFields command or the FilePrint and FilePrintDefault
commands.
See also: Getting help with calling Word's built-in dialogs using VBA (and why doing so can be much more useful than you'd think).
|