View Single Post
  #4   Report Post  
Max Moor
 
Posts: n/a
Default

"Shauna Kelly" wrote in
:

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word



It absolutely does! I've been working with Access in VB for quite
a while. I'm new to doing it with word. It's like starting over.

Could you tell me about how the Find executes the second (or
later) loop? Once Find has been successful, oRg bounds the found field,
and no longer encompasses the whole of the document.

I would think the second execution of Find would be only on oRg's
new range (the field previously found). How can it find any later
fields in the document, beyond its new, redefined range? (I hope this
question makes sense. It's really bugging me.)

- Max


Sub AddPageNumberRefs()
Dim oRg As Range
Dim oIns As Range
Dim strCode As String
Dim fldPg As Field

Set oRg = ActiveDocument.Range
oRg.TextRetrievalMode.IncludeFieldCodes = True
With oRg.Find
.Forward = True
.Format = True
.Style = ActiveDocument.Styles("Cross-reference Char")
.Text = "^d REF"
Do While .Execute
strCode = oRg.Fields(1).Code.Text
Set oIns = oRg.Duplicate
With oIns
.Collapse wdCollapseEnd
.Text = ", pg "
.Collapse wdCollapseEnd
Set fldPg = ActiveDocument.Fields.Add(Range:=oIns,
Type:
=wdFieldEmpty)
fldPg.Code.Text = " PAGE" & LTrim(strCode)
fldPg.Update
End With
Loop
End With
End Sub