Update All Fields in All Storys

A

Alex

Hi Guys

As you all probably know within TOOLS\OPTIONS\PRINT there
is an update fields and update links option before
printing. These options actually update all the fields /
links in all the stories of a word document.

Does anyone know how to reproduce this function, i.e.
updating all the fields in all the stories via VBA, that
way I can update my document fields whenever I want to
without having to print the document or without having to
update each field individually.

I've search the MS Knowledge Base and found the following
code, but it doesn't appear to work:

Sub UpdateAllFields()

Dim aStory As Range
Dim aField As Field

For Each aStory In ActiveDocument.StoryRanges
For Each aField In aStory.Fields
aField.Update
Next aField
Next aStory

End Sub

I've also checked the MVP site and found an article
referencing stories but my VBA skills are too basic to
understand the finer points of the article, i.e. how to
modify it.

Hope someone can help.

Big thank you in advance

Alex
 
J

Jay Freedman

Hi, Alex,

You don't have to print the document to take advantage of the "update
before print" -- all you need to do is go into Print Preview and back
to the document. If you want a macro for one-button convenience, use
this:

Sub UpdateAllFields()
ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview
End Sub
 
S

Suzanne S. Barnhill

I'm finding that often lately Print Preview *doesn't* update all fields
(TOC, for example).

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
Word MVP FAQ site: http://www.mvps.org/word
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
A

Alex

Hi Jay & Suzanne

I've tested the print preview method and as Suzanne
suggested, it doesn't update all the fields.

It updates the fields within the headers and footers
(first page, odd, even and seperated), it even updates
fields in textboxes within the headers and Footers (I use
these a lot as automatic draft watermarks).

But it doesn't update the TOC or fields within the main
document that refernce to document properties such as
Title, Subject etc.

Any ideas?

Alex
 
J

Jay Freedman

Hi, Alex,

Since no one method does it all, you'll have to code multiple steps.
First do the two-statement print preview dance that I mentioned
before. Then add special handling for the "slackers" (which are all in
the main story):

Dim oFld As Field
For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldTOC Or _
oFld.Type = wdFieldDocProperty Then
oFld.Update
End If
Next oFld

or, if you don't care whether some fields get updated twice, simply

Dim oFld As Field
For Each oFld In ActiveDocument.Fields
oFld.Update
Next oFld
 

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