Only update cross-reference fields in a WORD doc using VBA?

J

JoAnn

I'm using WORD 2000 with Windows 2000 and have all the
latest patches/service releases installed.

My doc contains a variety of fields. Everything except for
my cross references (which refer to page numbers) are
updated with a user form. These page number references do
not always remain accurate when end-users open the
document.

I'm not sure if this is something that would normally
happen whenever a user opens the document on their PC or
if this is a problem. In either case, would creating an
AutoOpen macro that selects the doc and updates all fields
every time the doc is open solve the problem?

Also ... is there a way to only update cross reference
fields in a document using VBA?

Thanks,
JoAnn
 
M

macropod

Hi JoAnn,

Here's a macro to update only REF fields:

Sub UpdateRefs()
If ActiveDocument.Fields.Count > 0 Then
For Each Fld In ActiveDocument.Fields
If Fld.Type = 3 Then Fld.Update
Next Fld
End If
End Sub

Cheers
 
P

Peter Hewett

Hi "macropod" <[email protected] (del NO.SPAM)>

Your solution is only partially correct as it only updates fields in the document body
(wdMainTextStory) not any other story range. To update all REF fields anywhere, use
something like this:

Public Sub UpdateAllRefFields()
Dim lngJunk As Long
Dim fldItem As Word.Field
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

' Only Update the Ref fields
For Each fldItem In rngStory.Fields
If fldItem.Type = wdFieldRef Then
fldItem.Update
End If
Next

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

HTH + Cheers - Peter
 
M

macropod

Hi Peter,

Yes, I was aware of that but, from the context of the original post, I
thought it unlikely that the REF fields would be anywhere other than in the
body of the document.

Cheers
 
J

JoAnn

Thank you both! I will give that a try.

-----Original Message-----
Hi Peter,

Yes, I was aware of that but, from the context of the original post, I
thought it unlikely that the REF fields would be anywhere other than in the
body of the document.

Cheers



updates fields in the
document body all REF fields
anywhere, use


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.627 / Virus Database: 402 - Release Date: 16/03/2004


.
 

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