I've had a macro to password protect documents for a while and it works
fine - except for one issue I just found. When the macro is run on a
document that was once protected for certain sections, only those
sections are protected. The only way I can protect all the sections is
to manually protect the form and individually check all of the sections
in the options. My current code is as follows:
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Password:="mypassword", NoReset:=False,
Type:=wdAllowOnlyFormFields
End If
So I was playing around to see how a macro would record if I did only
want to protect certain sections. By doing so, I noticed it has a new
line for each section. So if a document had 5 sections, and I wanted
to ensure all of the sections were protected, I would need the
following code:
On Error Resume Next
ActiveDocument.Sections(1).ProtectedForForms = True
ActiveDocument.Sections(2).ProtectedForForms = True
ActiveDocument.Sections(3).ProtectedForForms = True
ActiveDocument.Sections(4).ProtectedForForms = True
ActiveDocument.Sections(5).ProtectedForForms = True
ActiveDocument.Protect Password:="mypassword", NoReset:=False,
Type:=wdAllowOnlyFormFields
The "On Error Resume Next" command, btw, is needed to make sure I don't
get an error if the document has less than 5 sections.
I've done plenty of searching, but I can't find any code that is
simpler. I wanted to do something like
ActiveDocument.AllSections.ProtectedForForms = True
or
ActiveDocument.Sections(1 - 5).ProtectedForForms = True
but nothing seems to work. Is there a simpler way to write this macro?
Thanks in advance,
- Mike
Jay Freedman - 03 Aug 2006 18:32 GMT
Hi Mike,
Try it this way:
Dim sec As Section
For Each sec in ActiveDocument.Sections
sec.ProtectedForForms = True
Next sec
ActiveDocument.Protect Password:="mypassword", _
NoReset:=False, Type:=wdAllowOnlyFormFields
The For Each loop doesn't care whether you have one section, five, or a
hundred, it just loops through whatever is there.

Signature
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
> I've had a macro to password protect documents for a while and it
> works fine - except for one issue I just found. When the macro is
[quoted text clipped - 40 lines]
> Thanks in advance,
> - Mike
njmike@gmail.com - 03 Aug 2006 19:06 GMT
That was perfect, Jay. Thank you.
- Mike
> Hi Mike,
>
[quoted text clipped - 61 lines]
> > Thanks in advance,
> > - Mike