Automatically enter the text from a text field in headers

N

Nevets

Creating a form, with two text fields on page 1. The text fields are
bookmarked as EventName and EventDate. I would like to have the Event Name
and Date automatically appear on the second and subsequent page headers.
I've done this previously, but not within a header. I put the cursor in the
header where I wanted the EventName text to appear, hit Control + F9, then
typed EventName in the field. After hitting toggle field codes, I protected
the form and tested it. Nothing appears in the headers after I enter the
name in the EventName field and tab out. If I do the same thing somewhere
else in the document, it works. How do I get this to work in a header?
 
D

David Sisson

Inside header, Insert ->Field ->Ref, Select bookmark name

File -> Page setup -> Layout tab, check Different first page.
 
N

Nevets

What you suggested seems to make sense, but when I tried it, nothing happens.
I hit insert > Field > Ref, and among the list of bookmarks, selected
EventName and EventDate, which are the two bookmarks names of the first two
text fields in the document. Protected the form then saved it, then entered
text in the two fields. Nothing gets added in the header. If I hit Alt+F9,
I see { REF EventName } and { REF EventDate } where I expect to see the text,
but no actual text gets added when I tab out of either of the text fields.
For the sake of argument, I tried the same thing in a footer, and in the
body of the document, with no luck in either location. Also tried removing
everything else from a footer, but again, no luck.
 
G

Greg Maxey

The problem is that your field is not being update due to its location in a
header or footer. It you unprotect the form and go to the header, CTRL+a ,
F9 the field will update.

You can also see the update by opening and closing PrintPreview.

I would attach and onExit macro to the text field that runs:

ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview
 
F

fumei via OfficeKB.com

David said:
Inside header, Insert ->Field ->Ref, Select bookmark name

File -> Page setup -> Layout tab, check Different first page.

That does not work with 2002. I am not sure if it does with 2003. Using Ctl-
F9 and formfield name, or Insert > Field > Ref + bookmark name, in the header
will not update. Even with Calculate on exit checked for the formfield.

It will work, however, with other places in the MainStory. And that is the
issue.

You could add a wee OnExit macro to your formfield.

Sub UpdateHeaders()
Dim oHF As HeaderFooter
For Each oHF In ActiveDocument.Sections(1).Headers
oHF.Range.Fields.Update
Next
End Sub
 
G

Greg Maxey

fumei,

Concede that in this instance that would probably work, but as a point of
illustration put a shape in your header, right click the shape and select
add text and then put the field the shape.

You will see that, for reasons I don't understand, shapes contained in
headers and footers are not processed with the header or footer storyrange
or with the normal shape range. They have to be processed as a unique
collection.

See: http://gregmaxey.mvps.org/Field_Macros.htm

For this instance this code will work if run on exit:

With ActiveDocument
.PrintPreview
.ClosePrintPreview
End With
 
N

Nevets

How do I attach the on-exit macro. There is an option to add a macro on exit
from the text fields, but I don't know how to create/save your macro so that
it is available as an option in the drop-down.
 
G

Greg Maxey

You need to put this macro in the document project module:

Sub MyOnExit()
With ActiveDocument
.PrintPreview
.ClosePrintPreview
End With
End Sub

See: http://www.gmayor.com/installing_macro.htm

The with the form unprotected, double click the textfield and set "OnExit"
to run On Exit from the field.
 
N

Nevets

Figured out how to have your suggested macro run on exit from the text field,
but still having a small problem. I want the footer text to be small font
(8). I "Ctrl+a" within the footer, then choose 8 for the font size. When I
tab out of the EventName field, the text is entered, with the first word font
size 8, but the rest of the Event Name is size 12.

Any ideas?
 
G

Greg Maxey

I didn't test this but it should work.. Put your field in the footer like
this { REF EventName \* CharFormat } then select the "R" and format it as
8 point.
 
N

Nevets

In the footer field, it says the following: {REF EventName \*
MERGEFORMAT}, not CharFormat, so I'm not sure if your suggestion will work or
not.
In any event, I tried something else while waiting for your reply. I
selected all the text in the footer, made it 8 point, then updated the footer
style to match the selected text. This seems to have worked, in that when I
create a new form from the template and enter the EventName and EventDate
text, it comes out as 8 point font in the footers. I think I'll leave well
enough alone.
 
G

Greg Maxey

If it works don't fix it, but you could have changed \* MergeFormat to \*
CharFormat and did the rest and it should have worked.
 
E

eastlaketech

The macros & formatting works great on my computer. When I send the form to
someone else, it doesn't work. Any help would be appreciated.
 

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