Unprotect and Protect template when user enters a Formfield

F

Frank

Hi All,

I ran into another 'problem' with a protected template. I want the
protection switched off when a users tabs to a formfield (in my example
Description) so the user can change the style and the font of this Formfield.
After that, when he goes to the next formfield the protection should be
switched on again.
I have tried it with 2 macro's (and in the formfield switched the option Run
Marco on Entry and Exit)

Macro 1= ActiveDocument.Unprotect Password:="password"

This works fine!

Marco 2 (on Exit)
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True_
Password = "password"

The second macro doesn't work anymore. So the protection stays off when the
user leaves this Formfield. Can anyone help me with this?

Thanks in advance!

Kind regards,

Frank
 
J

Jay Freedman

Hi Frank,

If the code you showed for Macro 2 is exactly what's in your template,
it has a syntax error. At the end of the first line, after the word
True, you need a comma and a space before the underscore.

The comma is needed to separate the NoReset parameter from the
Password parameter, and the space is required to make VBA understand
that the underscore is a continuation character.

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

Charles Kenyon

An onexit macro is activated when the user leaves a formfield in a protected
form. Problem is, when you user exits, the form is not protected. You are
going to have to do something more elaborate.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide




--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
J

Jay Freedman

Sorry, I concentrated on the code and missed the fact that Charles
picked up. He's correct that the exit macro will never run if the
document is unprotected.

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

Frank

Hi Charles,

I know the Exit Macro doesn't work. Any idea what script to run after exit
the formfield description so the template is protected again.

Thanks!

KR

Frank

"Charles Kenyon" schreef:
 
C

Charles Kenyon

It may just be phrasing. The macro may very well work, it is that it will
not run. It isn't triggered automatically.

That is what I meant when I said that you are going to have to do something
more elaborate.

By the way, what is the point of protection if you are going to unprotect
upon entry to a field? At that point the user can change anything in your
form.

Simple solution would be to display the forms toolbar and tell the user how
to protect/unprotect.

Otherwise, I think you get into displaying a userform that has formatting
options for the field. Upon closing that UserForm, you unprotect, apply the
formatting to the field, and reprotect.
http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

For more about online forms, follow the links at
http://addbalance.com/word/wordwebresources.htm#Forms or
http://word.mvps.org/FAQs/Customization/FillinTheBlanks.htm especially Dian
Chapman's series of articles.

Hope this helps,
--

Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide




--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
J

Jean-Guy Marcil

Frank was telling us:
Frank nous racontait que :
Hi Charles,

I know the Exit Macro doesn't work. Any idea what script to run after
exit the formfield description so the template is protected again.

Instead of removing the protection when entering a field, you could give
your users the possibility of changing font attributes in all formfields.
Check out the sample code below. Create a toolbar (Or assign the font
attribute to the actual button on the Format toolbar - Add the macro the
Format toolbar, right click the Standard Bold button, "Copy Button Image",
then right click the button you created and right-click to paste the button
image, make the face "Default" - not text, remove the Word Bold button form
the Format toolbar. Of course, this will work best from a template.)

'_______________________________________
Public myRange As Range
'_______________________________________
Sub Sel_Bold()

Sel_Unprotect

myRange.Bold = wdToggle

Sel_Protect

End Sub
'_______________________________________

Sub Sel_Italic()

Sel_Unprotect

myRange.Italic = wdToggle

Sel_Protect

End Sub
'_______________________________________
Sub Sel_Underline()

Sel_Unprotect

If myRange.Underline = wdUnderlineNone Then
myRange.Underline = wdUnderlineSingle
Else
myRange.Underline = wdUnderlineNone
End If

Sel_Protect

End Sub
'_______________________________________
Sub Sel_Unprotect()

If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If

Set myRange = Selection.Range

End Sub
'_______________________________________
Sub Sel_Protect()

If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, _
Password:=""
End If

myRange.Select

End Sub
'_______________________________________


If you want to allow this type of editing in specific field, check the value
of the selected formfield name, and allow editing or not based on this.
See this code variation:

'_______________________________________
Public myRange As Range
'_______________________________________
Sub Sel_Bold()

If Sel_Unprotect Then

myRange.Bold = wdToggle

Sel_Protect

End If

End Sub
'_______________________________________
Sub Sel_Italic()

If Sel_Unprotect Then

myRange.Italic = wdToggle

Sel_Protect

End If

End Sub
'_______________________________________
Sub Sel_Underline()

If Sel_Unprotect Then

If myRange.Underline = wdUnderlineNone Then
myRange.Underline = wdUnderlineSingle
Else
myRange.Underline = wdUnderlineNone
End If

Sel_Protect

End If

End Sub
'_______________________________________
Function Sel_Unprotect() As Boolean

Sel_Unprotect = True

Select Case ActiveDocument.FormFields(Selection.BookmarkID).Name

Case "NoEdit", "OtherName", "ManyNames", _
"StillAnotherField", "Etc"

MsgBox "You cannot modify font attributes in this field.", _
vbExclamation, "No font editing"
Sel_Unprotect = False

Exit Function

Case Else

If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If

Set myRange = Selection.Range

End Select

End Function
'_______________________________________
Sub Sel_Protect()

If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, _
Password:=""
End If

myRange.Select

End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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