Inserting a field that users cannot edit

M

morau99

I am using custom doc properties to store some values and update
several fields in a word document after a particular event. I do not
want users to be able to overwrite any of the fields by typing. I
believe that this can be accomplished by selecting the field and
pressing ctrl+shift+F11, but how can I accomplish this
programatically?

(These are fields as in ActiveDocument.Fields)

Thanks!
 
L

Lene Fredborg

Ctrl+Shift+F11 unlocks fields.
Ctrl+F11 locks fields.

If the selection is in the main body of the document, you can lock all
fields in the main body via VBA as follows:
ActiveDocument.Fields.Locked = True
To unlock the fields, set Locked = false.

However, I actually think that locking the fields will do the opposite of
what you want: locking a field will _not_ prevent users from e.g. deleting
that field or from typing inside or deleting part of the field result. It
only prevents the field result from being changed even if you try to update
the field (F9) - and updating the field would actually bring back the correct
value of an _unlocked_ field.

What you could do instead:
In order to make it easy for the users to distinguish fields from normal
text so that they do not accidentally edit or delete the fields, you could
ask them to set the field shading to "Always" (via Tools > Options > View
tab) - or you could change the setting programmatically and inform the users
about this. The VBA code to use:
ActiveWindow.View.FieldShading = wdFieldShadingAlways

Locking fields can be useful in other situations. For example, if you have a
title field with a long title that you want to break in a particular place,
you can insert a manual line break in that position and then lock the field.

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

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