Help with code protecting Sections

E

Eric

I found this code posting and am having problems when using it. I want to
protect all evvan numbered sections and not the odd numbered sections. I am
getting a Compile Error: (Else without If) Error. Any help would be
appreciated, here is the code:

Sub reprotect()
Dim sPassword As String

For Each oSection In ActiveDocument.Sections
'If the section's index / 2 <> 0 (odd) protection needed'
If oSection.Index Mod 2 <> 0 Then oSection.ProtectedForForms = True
Else
'no protection needed(even)'
oSection.ProtectedForForms = False
End If
Next
If lProtected <> wdNoProtection Then
sPassword = "Review"

ActiveDocument.Protect Type:=lProtected, noreset:=True, Password:="Review"
End If
End Sub

Thanks
 
J

Jay Freedman

In the line

If oSection.Index Mod 2 <> 0 Then oSection.ProtectedForForms = True

put the cursor between "Then" and "oSection" and press Enter to split it
into two lines.

In VBA, an If statement in which the action following "Then" is on the same
line is complete; it can't take an Else or End If. Therefore, the Else on
the following line is considered to be an Else without an If.

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

Graham Mayor

I have just answered an almost identical question.

Dim i As Long
Dim Count As Integer
Dim sPassword As String
sPassword = "Review"

With ActiveDocument
Count = .Sections.Count
.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=sPassword
For i = 1 To Count
If i Mod 2 <> 0 Then
.Sections(i).ProtectedForForms = True
Else
.Sections(i).ProtectedForForms = False
End If
Next
End With

should so the trick

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
E

Eric

Whats even scarier, is the password shown in this code.... is also mine for
the Template........ Woe

That other question was from someone else? Or Am I forgetting when and what
I'm posting?
 
E

Eric

I saw that posting, I'm not crazy.... yet.

Graham, wondering what your code would look like if say you wanted Sections
(3), (4), and (7) protected?

Thanks
 
E

Eric

Another Scenerio:

I'm using alot of autotext. These autotext consist of form fields and the
user has the opportunity to enter the autotext throughout. Each autotext
contains a section break aswell. So each time they enter the autotext, a new
section is added as well.

Can I code something like:

"If Section (7) exists, then protect= True Else ignore"
"If Section (8) exists, then protect= False, Else ignore"
etc.

I should be paying you two!
 
J

Jean-Guy Marcil

Eric said:
I saw that posting, I'm not crazy.... yet.

Graham, wondering what your code would look like if say you wanted Sections
(3), (4), and (7) protected?

Then, you could use a Select Case like this:

Dim i As Long
Dim Count As Long
Dim sPassword As String
sPassword = "Review"

With ActiveDocument
Count = .Sections.Count
For i = 1 To Count
Select Case i
Case 3, 4, 7
.Sections(i).ProtectedForForms = True
Case Else
.Sections(i).ProtectedForForms = False
End Select
Next
.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=sPassword
End With
 
J

Jean-Guy Marcil

Eric said:
Another Scenerio:

I'm using alot of autotext. These autotext consist of form fields and the
user has the opportunity to enter the autotext throughout. Each autotext
contains a section break aswell. So each time they enter the autotext, a new
section is added as well.

Can I code something like:

"If Section (7) exists, then protect= True Else ignore"
"If Section (8) exists, then protect= False, Else ignore"
etc.

You need to explore the logic in more details here.

Can user add these Autotext anywhere in the document?

If so, they could, for example, add a new third section to a ten-section
document. So, after this insertion, the doucment now has 11 sections, and
each section index number that used to be from 3 to 10 is now 4 to 11. This
means that for those sections that change index number, the odd ones are now
even and the even ones are now odd.

How do you want to handle that?
In this scenario, which "new" sections need protecting?

If users can add sections, can they remove them as well? If so, what happens
to your protection scheme in that case?

Or, are users only allowed to add sections at the end?
 
E

Eric

Jean, you are correct and I have thought about the logic but am still
struggling with a solution.

The users are able to add in the middle of the document. If they unprotect,
they can remove aswell.

I'm trying to allow both, and "reduce", not restrict, the ability to protect
and unprotect.

Maybe I should remove the section breaks in the autotext. That way all
autotext additions will remain in a single section, grouped accordingly and
protection could be consistant. However each time they want to add, they
still will have to unprotect. Still fighting with a solution.

Can a macro to unprotect and then reprotect be attached to an autotext entry
somehow.

Thanks, you all are great! I'm pulling the winning lotto ticket numbers and
will forward them soon.
 
J

Jean-Guy Marcil

Eric said:
Jean, you are correct and I have thought about the logic but am still
struggling with a solution.

The users are able to add in the middle of the document. If they unprotect,
they can remove aswell.

I'm trying to allow both, and "reduce", not restrict, the ability to protect
and unprotect.

Maybe I should remove the section breaks in the autotext. That way all
autotext additions will remain in a single section, grouped accordingly and
protection could be consistant. However each time they want to add, they
still will have to unprotect. Still fighting with a solution.

Can a macro to unprotect and then reprotect be attached to an autotext entry
somehow.

I am struggling with this as well...
It seems you have a document that has some of its sections protected and
other unprotected.

The only interaction a user can have with a protected section is through
formfields.
Are you stating you wish to give user the ability to add the text from those
Autotext entries anywhere in the document? Even inside a formfield?

If that is the case, you will need a macro to assist users with that because
the insertion of Autotext is disabled in a protected section.

If not, then no need for anything special. In non-protected sections users
are allowed to insert AutoText. Just remove the section break from the
Autotext, unless that section break is necessary and cannot be replaced with
a "regular" next page type of break.
Thanks, you all are great! I'm pulling the winning lotto ticket numbers and
will forward them soon.

Never mind the ticket numbers, just send the cash! ;-)
 
E

Eric

The entire document is actually a form.

I would like to have some unprotected sections to allow inserting of
pictures, graphs etc. (not in a form field). There is no autotext inserted
inside a form field anywhere.

All of the autotext's however, are in "Form Field" format, thus the need to
protect after inserting.

The idea is for the user to add autotext: FormField (1)+ ForField (2) =
FormFiled (3).... and then be able to enter data in those fields. Just under
those fields would be an unprotected section that would all the inserting of
pictures etc.
the insertion of Autotext is disabled in a protected section.>>>

I think the macro to allow autotext insert/ unprotect+reprotect would be
helpful if I could call the macro prior to autotext insert. Then the idea of
removing the section breaks from the autotext would work great.

___________Start Section Protection_____________

Form Fields (Existing)

___________End Protected Section______________

...........Unprotected Section....... (Allow pictures etc.)
........... Also (allow insert of new Form Fields via Autotext)

___________Start (New Added)Section Protection_____________

Form Fields (Added via AutoText)

___________End Protected Section______________


Etc.

The Macro to allow the inserting of AutoText might be the key.


I think I'm getting closer!
 
G

Graham Mayor

It seems like you want your cake and your ha'penny? :) You appear to want to
have a document that provides the normal functions of a Word document,
whilst some of the document is protected. As you are discovering, this makes
for a really ugly and hard to maintain document template.

Either the document is a form in which case you have to accept the
limitations of the form format, or it isn't in which case you should
approach the problem from a different direction.

Why not create a user form to populate the parts of the document that you
wish to have populated and leave the document as a normal Word document
which would remove all the difficulties that you will struggle and fail to
overcome?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
E

Eric

I have thought about that in the early stages of the project. IS there a
link that discusses this method? I'd be happy to give that a try.

All of the information from the form will also be imported into Access. I
guess after they fill out the form, the form information can be
imported/exported and then they could go to the regular document and finish
with the bells and whisles.

Is there some informaiton on this method?

Thanks, I'll give it a try!
 

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