Password protect all sections in a document

N

njmike

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
 
J

Jay Freedman

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.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
N

njmike

That was perfect, Jay. Thank you.

- Mike

Jay said:
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.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top