Protecting a Section

L

LEU

The following macro will protect the whole document. How do I protect just
the first section of the document?

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True, Password:="whatever"


LEU
 
R

Russ

ActiveDocument.Sections(1).Range.Protect

The following macro will protect the whole document. How do I protect just
the first section of the document?

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True, Password:="whatever"


LEU
 
L

LEU

Thanks for answering Russ.

When I try the macro it gives me the following error message:
Method or Data member not found. When I hit debug, it highlights '.Protect'
at the end of the macro.

LEU
 
O

old man

Hi,

You want to do this:
ActiveDocument.Sections(1).ProtectedForForms = true

By the way when you turn on protected for forms all the sections default to
protected. You may want to unprotect all the other sections. When you insert
(or create) a new section after protection is on you should then set
protection on or off for the new section.

old man
 
O

old man

Hi,

When a section is protected it cannot be selected (only form fields in the
section can be selected). When I ran this code on a four section document
section 3 is protected. You can try this. After running it goto Tools -->
Protection then turn protection off and then click on the select sections
shortcut and you will see that section 3 is protected and protection is
turned on...

old man


Sub p1()
' protection is turned off at this point....
ActiveDocument.Sections(3).ProtectedForForms = True


ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

End Sub
 
O

old man

Hi,

When I work with protecting a document or template I do the following:

1) Create a document with multiple sections
2) Goto Tools --> Protect Document and int the drop down under Editing
Restrictions select Filling in Forms.

3) then I select what sections should be protected and which should not be.

4) These sections are protected when protection is turned on through code or
through pressing the button Yes, Start Enforcing Protection, or in the Forms
Toolbar clicking on the Protect Forms Icon (it looks like a lock on is
typically on the Viewers extreme right).

old man
 
L

LEU

Old man,

I did all that you said to do on my template and when I create a new
document the protection of the first section works fine. What I was trying to
do is when we convert our old documents over to our new template. My macro
deletes the old first section from the existing document and inserts the new
templates first section. Then I want to protect the first section so my
endusers will have to use the form to add, edit or delete information. That
way its done the same way every time. The total number of sections will very
from document to document.

LEU
 
O

old man

Hi,

Try running the code until the point after you have deleted the section and
added/replaced the section. Stop. View the document and see what is the
section number of the section that was added. You may may not be setting
protection on the section you think you are. Once a section is protected or
not protected inserting sections do not change the protection status.

old man
 
L

LEU

OK, I have stopped the macro after replacing the section. How do I see the
section number of the section that was added?

LEU
 
O

old man

Hi,

At this point run the code that I send you (after turning off protection).
Does it turn protection on for forms for section 1?

ActiveDocument.Sections(1).ProtectedForForms = True

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

By the way through code you can use the information property of the range,
or selection, object to find out what section it is in e.g.:

Selection.Information(wdActiveEndSectionNumber)

will return the section number of whatever is currently selected.

Old Man
 
L

LEU

I ran the code and it protected the whole document. I have been playing with
the following macro. Each time I run it, it unprotects the next section. I
just can't get it to loop through all the sections. If it would I think it
would work.

Dim idx As Long
Dim num As Long
Dim aVar As Variant
Dim Sec As Section

For Each aVar In ActiveDocument.Variables
If aVar.Name = "idx" Then
num = aVar.Index
Exit For
End If
Next aVar

If num = 0 Then
ActiveDocument.Variables.Add Name:="idx", Value:=1
End If
idx = ActiveDocument.Variables("idx") + 1

For Each Sec In ActiveDocument.Sections
ActiveDocument.Sections(idx).ProtectedForForms = False
ActiveDocument.Variables("idx").Value = idx
Next

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

End Sub


LEU
 
R

Russ

LEU,
Do your section swap, then:
From what I understand and tried to tell you, the procedure is to protect
the whole on-line form, then unprotect the sections you want unprotected.

Dim Sec As Word.Section
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
For Each Sec In ActiveDocument.Sections
If Sec.Index = 1 Then 'Or Sec.Index = 5 Then, etc.
Sec.ProtectedForForms = False
Exit For 'Don't use if unprotecting more than 1 section
End If
Next
 
R

Russ

LEU,
I lost track of your original post request. If you want just leave the first
section protected, then use this:

Dim Sec As Word.Section
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
For Each Sec In ActiveDocument.Sections
If Sec.Index <> 1 Then
Sec.ProtectedForForms = False
End If
Next
 
L

LEU

old man and Russ,

I changed it just a little and it worked. The first section is protected and
the rest are not. Here is what i did:

Dim Sec As Word.Section
For Each Sec In ActiveDocument.Sections
If Sec.Index >= 2 Then
Sec.ProtectedForForms = False
End If
Next
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True, Password:="whatever"

I have a new problem now with the first section protected. Some of my mouse
functions are not working. When I right mouse click on my numbering in the
unprotected section of my document the Restart Numbering, Continue Numbering,
Bullets and Numbering, Cut and Copy selections on the menu are grayed out.
There is no numbering in the first section. Any ideas why that would be
happening?

LEU
 
R

Russ

LEU,
This is the third time I have said this, protect the whole document first,
then unprotect the sections you want unprotected. Your code snippet is doing
it backwards.
 
R

Russ

LEU,
If you go into the VBA Editor, Alt/F11, and place the cursor in a word and
press the F1 function key, VBA help will try to bring up help on that word.

Here's what VBA help says for ProtectedForForms.
ProtectedForForms Property:
True if the specified section is protected for forms. When a section is
protected for forms, you can select and modify text only in form fields.
Read/write Boolean.
Note To protect an entire document, use the Protect method of the Document
object.
ProtectedForForms Property Example
This example protects the second section in the active document for forms.
If ActiveDocument.Sections.Count >= 2 Then _
ActiveDocument.Sections(2).ProtectedForForms = True
This example unprotects the first section in the selection.
Selection.Sections(1).ProtectedForForms = False
This example toggles the protection for the first section in the selection.
Selection.Sections(1).ProtectedForForms = Not _
Selection.Sections(1).ProtectedForForms

Here's what VBA help says for Protect.
Protects the specified document from changes. When a document is protected,
the user can make only limited changes, such as adding annotations, making
revisions, or completing a form.
Note If the document is already protected when you use this method, an
error occurs.
Syntax
expression.Protect(Type, NoReset, Password)
expression Required. An expression that returns a Document object.
Type Required Long. The protection type for the specified document. Can be
one of the following WdProtectionType constants: wdAllowOnlyComments,
wdAllowOnlyFormFields, wdAllowOnlyRevisions, or wdNoProtection.
NoReset Optional Variant. False to reset form fields to their default
values. True to retain the current form field values if the specified
document is protected. If Type isn't wdAllowOnlyFormFields, the NoReset
argument is ignored.
Password Optional Variant. The password required to "unprotect" the
specified document.

Protect Method Example
This example protects the active document for forms without resetting the
contents of the form fields.
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
This example protects Monthly Report.doc so that only comments can be added
to it. The password "free" is required to unprotect the document.
Set myDoc = Documents("Monthly Report.doc")
myDoc.Protect Type:=wdAllowOnlyComments, Password:="free"
 
L

LEU

Russ,

Again, Thank you for all your help.

Now on your protect macro. If I ran the macro like you have it, with the
added password, it gives me an error telling me that the document is already
password protected and it can not change things. When I changed it to tell it
which sections to protect and not protect first, then protect the whole
document, it worked perfectly. I tried it on several documents and on
different computers and it worked every time. The first page is protected and
the rest are not.

LEU
 

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