View Single Post
  #5   Report Post  
Shauna Kelly
 
Posts: n/a
Default

Hi Max

I think Jezebel has answered the question.

It's really bugging me.

As a Sunday evening brain teaser, create a new document, type =rand(3,2),
press Enter, and step through the following code watching what happens to
the document:

Sub Test1()

Dim oRange As Word.Range
Set oRange = ActiveDocument.Range

With oRange.Find
.Text = "brown"
.Wrap = wdFindStop
Do While .Execute
oRange.Bold = True
Loop
End With

With ActiveDocument.Range.Find
.Text = "brown"
.Wrap = wdFindStop
Do While .Execute
ActiveDocument.Range.Bold = True
Loop
End With

End Sub


Hope this helps.

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


"Max Moor" wrote in message
6...
"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