UpdateAll fields code won't update header

K

Kamran

Hello,
I want to use the code below to populate a header with info in Word form.
There are two fields. In the second field I have selected the macro for "Run
macro on Exit", but it doesn't work. The header fields stay unchanged. What
am I missing?

[From http://www.gmayor.com/installing_macro.htm]

Sub UpdateAll()
Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing
End Sub
 
M

macropod

Hi Kamran,

Try adding the following lines to your code:
Application.ScreenUpdating = False
ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview
Application.ScreenUpdating = True
 
K

Kamran

I did away with the rest of the code and used what you provided and .....
SHAZAM!
Sheer genius. Thanks for the help.
 
G

Graham Mayor

The macro should work with fields in the header - it does here, but headers
can be problematical. Use this code instead. Fields in the body of the form
will be updated if you check the calculate on exit check box of the last
relevant field.

Sub UpdateHeaderFooter()
Dim oField As Field
Dim oSection As Section
Dim oHeader As HeaderFooter
Dim oFooter As HeaderFooter
For Each oSection In ActiveDocument.Sections
For Each oHeader In oSection.Headers
If oHeader.Exists Then
For Each oField In oHeader.Range.Fields
oField.Update
Next oField
End If
Next oHeader
For Each oFooter In oSection.Footers
If oFooter.Exists Then
For Each oField In oFooter.Range.Fields
oField.Update
Next oField
End If
Next oFooter
Next oSection
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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