Update Fields Automatically

K

Kevin

I have a number of fields in my documents routinely. They are Table
references, FIgure References, etc. that are automatically numbered as well
as the Table of Contents. If I delete say a table in the middle of a
partially drafted document the tables below the point where I deleted the
table do not renumber. I use the following code to renumber all numbered
objects in my document:

Dim fldLoop As Field
Dim myCount As Integer

myCount = 1

For Each fldLoop In ActiveDocument.Fields
fldLoop.Select
Selection.Fields.Update
myCount = myCount + 1
Next fldLoop
MsgBox "Done!," & myCount & "Fields updated"

This works fine for what it is intended to do but it takes the focus in the
document to the last field renumbered which takes me away from the part of
the document I am working on. This is not a problem if the document is short,
but can be a real pain in the rump if the document is long. Is there a way to
determine the current location within my document and return there when done.

FYI... I tried a slightly different script which used the collection and did
not change the current location, but it would not work. Nothing was updated.

Thanks in advance for the help!
 
D

Dave Lett

Hi Kevin,

Yes, there is. It's pretty simple once you know how to do it.

Dim oRng As Range
Dim fldLoop As Field
Dim myCount As Integer

myCount = 1
'identify your current selection
Set oRng = Selection.Range
For Each fldLoop In ActiveDocument.Fields
fldLoop.Select
Selection.Fields.Update
myCount = myCount + 1
Next fldLoop
'reselect the original selection before the fields update
oRng.Select
MsgBox "Done!," & myCount & "Fields updated"

HTH,
Dave
 
D

Dave Lett

Hi Anne,

Well, for the most part, Printing and Print Preview will update all the
fields (which presumes you have the Update Fields checkbox selected on the
Print tab of the Options dialog box). However, this is not true for all
fields. For example, if you have an IncludeText field that references a Page
field in another document, then this must be updated via CTRL + A or the
right-click method.

HTH,
Dave
 
K

Kevin

That helps but I would rather run a macro that updates everything but returns
me to the point where I had been working. I would be far simpler if I can
figure out the vba code to do it.
 
K

Kevin

That did it!

Thanks!

Dave Lett said:
Hi Kevin,

Yes, there is. It's pretty simple once you know how to do it.

Dim oRng As Range
Dim fldLoop As Field
Dim myCount As Integer

myCount = 1
'identify your current selection
Set oRng = Selection.Range
For Each fldLoop In ActiveDocument.Fields
fldLoop.Select
Selection.Fields.Update
myCount = myCount + 1
Next fldLoop
'reselect the original selection before the fields update
oRng.Select
MsgBox "Done!," & myCount & "Fields updated"

HTH,
Dave
 
J

Jonathan West

Dave Lett said:
Hi Kevin,

Yes, there is. It's pretty simple once you know how to do it.

Dim oRng As Range
Dim fldLoop As Field
Dim myCount As Integer

myCount = 1
'identify your current selection
Set oRng = Selection.Range
For Each fldLoop In ActiveDocument.Fields
fldLoop.Select
Selection.Fields.Update
myCount = myCount + 1
Next fldLoop
'reselect the original selection before the fields update
oRng.Select
MsgBox "Done!," & myCount & "Fields updated"

You don't need the selection flitting all over the place. Much quicker will
be to work as follows

Dim fldLoop As Field
For Each fldLoop In ActiveDocument.Fields
fldLoop.Update
myCount = myCount + 1
Next fldLoop
MsgBox "Done!," & ActiveDocument.Fields.Count & "Fields updated"


or it could perhaps be done even with a one-liner

ActiveDocument.Fields.Update


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
C

Charles Kenyon

I believe both your code and David's will update all fields in the body of
the document, but not headers/footers.

The following updates all the fields and shouldn't move the insertion point.

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
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: 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.
 

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