Fields.Update does not work correctly in Word 2007

K

Karl Zuern

Hi,

I am looking for a VBA-Code to update all fields in a document.

The command "ActiveDocument.Fields.Update" does not update fields in headers
and footers and in text-fields.

In Word 2003 the following code does the job perfectly:
-------------------------------------------------------------
For Each Teil In ActiveDocument.StoryRanges
Teil.Fields.Update
While Not (Teil.NextStoryRange Is Nothing)
Set Teil = Teil.NextStoryRange
Teil.Fields.Update
Wend
Next
---------------------------------------------------------------

But in Word 2007 this code does not work. Fields in headers, footers an
text-fields will not update.
What happens here?

Does anybody have an idear how to consequently update all fields?

Regards Karl.
 
D

Doug Robbins - Word MVP

Quick and dirty way is to use

With ActiveDocument
.PrintPreview
.ClosePrintPreview
End With
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
K

Karl Zuern

Hi Doug,

good idea.

But it seems not to work. This updates fields in the header very well. But
it does not update Fields in the normal text or in text-fields. ????????

There is a word option "Update Fields before Printing", but this does not
change anything.

I use fields to show DOCVariables in the document. They do not update, when
the variable chances.
Sample:
{DOCVARIABLE "Copyright" \*MERGEFORMAT}

Updating of fields seems to be a great problem. I have found a lot of
statements in the web. But with Word 2007 no method seems to work correct.

Thanks for your help.
Regards Karl.
 
S

Stefan Blom

Does it work if run Doug's code *and* your code?

--
Stefan Blom
Microsoft Word MVP


in message
 
K

Karl Zuern

thanks to Stefan and Doug!
Stefan, you are right! The combination of both codes works!

But now, I found a solution without switching to Print-Preview. This
solution should »really« update all fields in the document (hopefully).

---------------------------------------------------
Sub UpdateAllFields()
'Update all fields in all parts of the document
' in Maintext, in footer / Headers, in textboxes
' and also in textboxes inside footers and headers
Dim aRange As Range
Dim aShape As Shape

Application.ScreenUpdating = False
'Update filds in alle Parts of the document (Header, Footer etc...)
For Each aRange In ActiveDocument.StoryRanges
aRange.Fields.Update

'------------------------
'Fields.Update does not work, if fields are inside textboxes, when the
Textbox is inside a footer / header
'to fix this separatly handle the Textboxes (ShapeRange)
For Each aShape In aRange.ShapeRange
With aShape.TextFrame
If .HasText Then
.TextRange.Fields.Update
End If
End With
Next aShape
'------------------------

While Not (aRange.NextStoryRange Is Nothing)
Set aRange = aRange.NextStoryRange
aRange.Fields.Update
Wend
Next
Application.ScreenUpdating = True

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