Range Question

G

Greg

Please open and new blank document and type three paragraphs:

Test
Test
Test

Now run this code:

Sub Test2()
Dim oRng As Word.Range
Dim i As Long
Set oRng = ActiveDocument.Paragraphs(1).Range
MsgBox oRng
With oRng.Find
.Text = "Greg"
While .Execute
i = i + 1
Wend
End With
MsgBox i
End Sub

While the range is clearly set to paragraph(1), the text is found three
times.

What is the explanation for this behavior.

Thanks
 
K

Klaus Linke

Hi Greg,

oRng.Find.Execute usually resets oRng to the first occurrence of the search
text within oRng.
But if oRng.Text already *is* the search text, it'll continue the search
downwards.

You can watch that if you put oRng.Select in the While...Wend loop, and
single-step through the macro.

This makes sense if you realize that .Find mimicks the behaviour of EditFind
in the user interface.
Else, it sure is confusing behaviour...

Greetings,
Klaus
 
G

Greg Maxey

Klaus,

I still don't get it. Perhaps I gave a poor example:

Use these three paragraphs:

This is a test of this test paragraph
This is a test of this test paragraph
This is a test of this test paragraph

Now run either:

Sub Test1()
Dim oRng As Word.Range
Dim i As Long
Set oRng = ActiveDocument.Paragraphs(1).Range
MsgBox oRng
With oRng.Find
.Text = "Test"
While .Execute
oRng.Select
i = i + 1
Wend
End With
MsgBox i
End Sub

Sub Test2()
Dim oRng As Word.Range
Dim i As Long
ActiveDocument.Paragraphs(1).Range.Select
MsgBox Selection.Range
With Selection.Find
.Text = "Test"
While .Execute
i = i + 1
Wend
End With
MsgBox i
End Sub

In both cases the defined range is disregarded and the macro returns a value
of 6.


If I select the first paragraph and run Find from the UI it finds the 2
instances in the paragraph, stops, and fires the wrap prompt. That is the
part that seems to be missing in the VBA version.

I was trying to solve another issue had to do some silly stuff with the
defined range to get the desired result. Something like this:

Sub Test3()
Dim oRng As Word.Range
Dim oRngDup As Word.Range
Dim i As Long
Set oRng = ActiveDocument.Paragraphs(1).Range
Set oRngDup = oRng.Duplicate
oRng.Font.Color = wdColorBrightGreen
MsgBox oRng
With oRng.Find
.Text = "Test"
.Font.Color = wdColorBrightGreen
While .Execute
oRng.Select
i = i + 1
Wend
End With
oRngDup.Font.Color = wdColorAutomatic
MsgBox i
End Sub

Seems that there should be some way to limit the scope of the search to the
defined range. What am I missing?/
 
K

Klaus Linke

After the first execution, oRng is the range of the first match (This is a
*test* of this test paragraph¶...).

The second time you execute the Find, Word searches that range.
Since it sees that the text of oRng already is "test", the range is
collapsed to the end, and Word continues searching down, matching the next
"test".

If you want to avoid that, reset the range after the first execution... from
the end of the match (so you don't match that again) to the end of the
original oRng.

It's much the same with the Selection in Test2.

Greetings,
Klaus
 
G

Greg Maxey

Thanks Klaus,

A vague recollection is whispering that you have taught me this once before
;-).

So, applying this to the other fellows post about highlighting two or more
commas in each sentence then something like this might do:

Sub Test2()
'Best way so far.
Dim i As Long, j As Long, k As Long
Dim oRng As Word.Range
Dim oRngDup As Word.Range
Dim oTmpRng() As Word.Range
For i = 1 To ActiveDocument.Sentences.Count
j = 0
Set oRng = ActiveDocument.Sentences(i)
Set oRngDup = oRng.Duplicate
With oRng.Find
.Text = ","
While .Execute
j = j + 1
ReDim Preserve oTmpRng(j)
Set oTmpRng(j) = oRng.Duplicate
k = oRng.End
oRng.Start = k + 1
oRng.End = oRngDup.End
Wend
End With
If j > 1 Then
For j = 1 To UBound(oTmpRng)
oTmpRng(j).HighlightColorIndex = wdYellow
Next j
End If
Set oRng = Nothing
Next i
End Sub
 

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