Run Macro in Protected as Form document

R

RealGomer

I tried to create a macro that would copy the information
in one form field into another. I named each form field as
a bookmark and then used Tools - Macro - Record Macro.
My macro recording was
F5
Bookmark: TPName
Copy
F5
Bookmark: CopyTPName
Paste
End Recording

I saved the macro in the document proper. I protected the
docuemnt as a form and then saved the document. The
properties for Bookmark: CopyTPName were set to Run Macro
on Enter.
Every time I tested the macro, I received an error message
that the Go To subject could not be found. When I ran the
debugger, the Go To Subject name was the same as the
bookmarks!
Any suggestions? I want to use this document for
addressing form letters, so the users will only need to
enter the TPName once instead of twice.
Thank you.
(PS - Please excuse any typos. My screen was washed out by
the sun and I don't have a glare shield.
 
C

Charles Kenyon

First, do you really need another form field to hold the same information?
Is it something that you want the user to be able to change (in the second
but not the first field)? If not, look into using a REF field in the body of
your document where you now have the second form field. In your original
form field, check the properties to activate "Calculate on exit." The
properties dialog will also show you the bookmark name for that field (to be
used by the REF field); you can, and probably should, change that name to
something more meaningful. Information put into your first formfield will be
automatically reflected in the REF field when the user tabs out of your
first formfield.
--

Charles Kenyon

See the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
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.
 
D

Doug Meyer

This can be done (the following code snippets will do it), but it really amounts to a sort of a hack: a set of kluges wrapped inside a bunch of work-arounds. I really don't recommend doing things this way. Anyhow, here is how I would do what you've requested

NB: the first three Subs correspond to each of the three FormFields that I wanted to replicate on the second page. The rubber hits the road in DuplicateMyField()

code follows---code follows---code follows---code follows---code follows---code follows---code follows--

Sub DuplicateStudentName(

Call DuplicateMyField("StudentName", "StudentNameDuplicate"

End Su

Sub DuplicateStudentID(

Call DuplicateMyField("StudentID", "StudentIDDuplicate"
End Su

Sub DuplicateCurrentDate(

Call DuplicateMyField("CurrentDate", "CurrentDateDuplicate"

End Su

Sub DuplicateMyField(strSource As String, strTarget As String

Dim strText As Strin

'Capture the text portion of the FormField ONL
strText = ActiveDocument.FormFields(strSource).Range.Tex

'Move to the target are
ActiveDocument.Bookmarks(strTarget).Selec

'(Temporarily) remove the protection, if it is presen
If ActiveDocument.ProtectionType > 0 The
ActiveDocument.Unprotec
End I

'Replace the text that was in the target bookmark with the text copied from the FormFiel
Selection.Text = strTex

'Replace the bookmark itself (the assignment of strText was destructive
Selection.Bookmarks.Add (strTarget
'Move back to the source are
ActiveDocument.Bookmarks(strSource).Selec

'Re-Protect the form field
If ActiveDocument.ProtectionType = wdNoProtection The
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=Tru
End I

End Su

code ends---code ends---code ends---code ends---code ends---code ends---code ends---code ends--

The above code can only run (AFAIK) when security is lowered to allow macros to run, or when there is a signed certificate. Troublesome

Also, whenever the target bookmark is selected, the focus will move there. If your Word instance isn't large enough to show both the source and target ranges (bookmarks) simultaneously, then executing the macros will cause it to appear to jump around. The screen can be disabled while the macro is running, but that just adds more complexity to an already complex set of kluges. More things to break, you know

For what you are requesting, it would work much simpler to employ cross-references with your Word FormFields app. Just set up the FormFields as you normally would, but also chedk the Compute on Exit checkbox (I think that's what it's called). Then position your cursor wherever you want to have the text copied to, and Insert CrossReferences (both are menu picks). Voila. (This also works inside headers and footers, I'm told)

Clearly, the cross-referencing technique can't work when you need to programmatically transform your entry in some way, so it's limited. But it is definitely more elegant than the Macro method above for the purpose you've spelled out

(Here is how you might want to insert a cross-reference programattically:

code begins---code begins---code begins---code begins---code begins---code begins--

Sub InsertCrossReference(

Selection.InsertCrossReference ReferenceType:="Bookmark", ReferenceKind:=
wdContentText, ReferenceItem:="CurrentDate", InsertAsHyperlink:=False,
IncludePosition:=False, SeparateNumbers:=False, SeparatorString:="

End Su

code ends---code ends---code ends---code ends---code ends---code ends---code ends--

Hope this help

Doug Meyer
 
R

RealGomer

Thanks, guys! No offense, Doug, but Charles solution
seems simpler. As I often sign my postings, "I know enuff
to dangerous." Which translates to my following KISS (not
the musical group tho' they do kick.)
 
R

RealGomer

Well, Charels, I did as you said. Unfortunately, I now
cannot protect the document as a form. The Protect
document is greyed out, even after I deleted the Ref
field. Suggestions?
 
C

Charles Kenyon

Hi,

A Ref field isn't a problem in protecting a form. It has to be something
else. This isn't a mailmerge document is it?

Can you access the Forms toolbar and use the padlock button?

Which version of Word are you using?

--

Charles Kenyon

See the MVP FAQ: <URL: http://www.mvps.org/word/> which is awesome!
--------- --------- --------- --------- --------- ---------
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.
 

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