How to lock specific field

C

Claudia

I'm a VBA newbie and I'm having trouble finding code that will lock a
specific field at the end of my subroutine. All other fields in my template
can be updated when the document is saved or printed, but there is one field
that should not update once the user has entered information into it. All I
want to do is incorporate code perhaps with an exit macro that will prevent
it from updating when the document is saved as a new document or when it's
printed (if Update Fields is selected in the user's options). I'd be grateful
for any help. Thank you.
 
J

Jay Freedman

I'm a VBA newbie and I'm having trouble finding code that will lock a
specific field at the end of my subroutine. All other fields in my template
can be updated when the document is saved or printed, but there is one field
that should not update once the user has entered information into it. All I
want to do is incorporate code perhaps with an exit macro that will prevent
it from updating when the document is saved as a new document or when it's
printed (if Update Fields is selected in the user's options). I'd be grateful
for any help. Thank you.

Are the fields in your document actually form fields in a protected
form, or are they just fields? Only form fields have the possibility
of assigning an exit macro, which is what makes me think that's what
you're talking about.

If that's correct, this code assigned as the exit macro for the Text1
form field will "lock" (actually, disable) the field:

Sub LockText1()
ActiveDocument.FormFields("Text1").Enabled = False
End Sub

Of course, change the name in quotes to match the actual name of the
field.

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

Claudia

I built in a code that unprotects the doc once the user has entered all the
info. While the doc is Protected, it's easy for the user to tab through all
of the formfields, and some of those formfields Calculate on Exit so they
populate other areas with bookmark references.

This works great, but because this is a law firm and these documents are
actually legal briefs, the document then has to be unprotected so the
attorney or his/her assistant can enter the language of the brief. Therefore,
I made it so when the user exits the last field code, the document is
unprotected.

Of course, the problem is we risk losing all of the info entered into the
formfields when the user sends the doc to print or when they save to a new
document. I know when this happens to me, Undo will bring it all back and
that I can lock the fields I don't want to update. But many of our users
don't want to have to do that.

So, is there a simple code that locks certain fields so they don't update?
If so, I can place that line of code just before the one instructing the
document to unprotect.
 
J

Jay Freedman

Yes, that can be done. You have to approach it a bit differently,
though:

ActiveDocument.Bookmarks("Text1").Range.Fields(1).Locked=True

Again, change "Text1" to the actual name of the form field. This takes
advantage of the fact that the "bookmark name" of a form field really
is a bookmark. A field locked in this way won't respond to an update
with F9 when 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.
 
C

Claudia

Thank you, that worked perfectly. What's more I can now use the same code for
other templates as well as the one that prompted my inquiry.
 

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