Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2006

Tip: Looking for answers? Try searching our database.

Password protect all sections in a document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
njmike@gmail.com - 03 Aug 2006 16:37 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.