macro to update field

R

raniet

How do I get automatically updated fields in the header? I have a template
file that is large. I would like the Heading 1 (i.e. Introduction, Chapter
1, etc.) to appear in the header. I would like the header to automatically
update from a single macro instead of the recommended way of changing the
previous header manually. I am also a novice when it comes to creating Word
Macros
 
G

G.G.Yagoda

The easiest way to update all fields in a document is to switch to the
Print Preview view then back.

ActiveDocument.PrintPreview
Selection.EscapeKey

Slightly crude, but it works.
 
D

Doug Robbins

To close Print Preview you can use

ActiveDocument.ClosePrintPreview

Of course using Print Preview will only work if the Update Fields option is
set under Tools>Options>Print. If it is not, you will need to iterate
through all of the headers footers in the document using

Dim i as Long
For i = 1 to ActiveDocument.Sections.Count
With ActiveDocument.Sections(i)
.Headers(wdHeaderFooterFirstPage).Range.Fields.Update
.Headers(wdHeaderFooterPrimary).Range.Fields.Update
.Footers(wdHeaderFooterFirstPage).Range.Fields.Update
.Footers(wdHeaderFooterPrimary).Range.Fields.Update
End With
Next i


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
C

Charles Kenyon

Rather than use a macro, consider using the StyleRef field. In my experience
(at least in Word 2003) the StyleRef field is one of the few fields in Word
that seems to update automatically.

The following updates fields in headers and footers as well as in the main
doucment.

Private Sub FieldsUpdateAllStory()
' All Story Field Updater
' Written by Charles Kyle Kenyon 9 December 2004
' repaired with help from Jezebel 10 December 2004
' Note, if used in protected form this will reset
' formfields to their defaults
Dim oStory As Range
On Error Resume Next
For Each oStory In ActiveDocument.StoryRanges
Do
oStory.Fields.Update
Set oStory = oStory.Next
Loop Until oStory Is Nothing
Next
End Sub
 

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