PDA

View Full Version : Protecting a document using a macro



Giuliar
06-09-2008, 12:06 PM
There is already a post on the forum about unprotecting a document, but I tried it and it didn't work. I have a customized template with macros in it and the template also has a checkbox form in it. When I protect the document, my macros don't work, so I need to protect that document after it has been initialized. Can someone give me sample code to protect the document after the macros have run so that the checkboxes can be used. Thanks.

By the way, I just started creating templates and macros in Word a few months ago, so there's a lot I don't know.

fumei
06-09-2008, 12:31 PM
"about unprotecting a document, but I tried it and it didn't work. "

WHAT post?

What does "didn't work" mean?

"has a checkbox form in it." What kind of checkbox? A formfield checkbox (from the Forms toolbar), or an ActiveX checkbox (from the Controls toolbar)?

"When I protect the document, my macros don't work"

Macros can, and do, work when a document is protected. Although some do not work fully, or properly, when the document is protected. It depends. For example, an OnExit macro for a formfield will work when the document is protected. However, some instructions will NOT work when a document is protected.

In any case:Sub Whatever()
' some instructions
ActiveDocument.Protect (wdAllowOnlyFormFields), _
NoReSet:=True, Password:=""
End Sub

will process the "some instructions", then protect the document for Forms, keep the current values of the formfields (NoReSet:=True), and set the Password as blank ("").

Humbled
07-18-2008, 11:14 AM
I just finished a form using the forms toolbar (not userforms). My document contains a mix of checkboxes, text fields, and dropdowns.

This code seems to be working just fine:

Sub ResetForm()
'
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End if 'This is actually on 1 line in my macro

With ActiveDocument
.FormFields.Shaded = True
.Protect Type:=wdAllowOnlyFormFields, noreset:=False
End With
End Sub

fumei
07-18-2008, 12:59 PM
Just a hint. Make sure you have NoReset equal the way you want it.

You have it as False. This means all existing data in all the formfields is cleared. If you wish to retain any data already existing, you need to make NoReset:=True.

Humbled
07-18-2008, 02:09 PM
Yes - I should have described my intent a little better.

I wanted a macro that would reset the fields to thier initial state - since my form is protected except for fields I needed the unprotect/protect to make it work. (This form will be used by other users and a button is simpler than having them open the forms toolbar).

Thanks for the input ... same reasoning applies to the .shaded parameter as well - I happen to like shading, others may not.