Update Fields

K

Kas

I need to write a macro that will update all fields in
the document. Is there a way to do this?

Any help would be greatly appreciated.
 
C

Charles Kenyon

All fields in the body of the document is easy. You need the following in
your code:

ActiveDocument.Fields.Update

If the fields are in headers/footers or text boxes something more is needed
and it would help to know what the fields are because some can be updated by
shifting into Print Preview.

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.
 
M

macropod

Hi Kas,

Try the following macro:

Sub RefreshFields()
Dim oSection As Section
Dim shp as Shape
Dim oHeadFoot As HeaderFooter
ActiveDocument.Fields.Update
For Each oSection In ActiveDocument.Sections
For Each oHeadFoot In oSection.Footers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Fields.Update
Next
For Each oHeadFoot In oSection.Headers
If Not oHeadFoot.LinkToPrevious Then oHeadFoot.Range.Fields.Update
Next
Next
If ActiveDocument.Shapes.Count > 0 Then
For Each shp In doc.Shapes
With shp.TextFrame
If .HasText Then
.TextRange.Fields.Update
End If
End With
Next
End If
End Sub

Cheers
PS: watch out for e-mail word-wraps in the above code
 
E

eNsOh

These should too.
I have found them in some earlier other anwsers.
Havn't tryed them yet.

'------------------------------- Code 1
Public Sub UpdateAllFields1()
Dim lngJunk As Long
Dim rngStory As Word.Range

' Word missing first Header/Footer bug workaround
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType

' Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges

' Iterate through all linked stories
Do
' Do Update all fields
rngStory.Fields.Update

' Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
End Sub
'------------------------------- Code 1

'------------------------------- Code 2
Public Sub UpdateAllFields2()
Dim pRange as Word.Range
Dim pRange2 as Word.Range

For each pRange in ActiveDocument.StoryRanges
Set pRange2 = pRange
Do until pRange2 is nothing
pRange2.UpdateFields
Set pRange2 = pRange2.NextStoryRange
Loop
Next
End Sub
'------------------------------- Code 2

Ebbe
 

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