using repeat function for search

M

muyBN

If I have to do a search for 20 pieces of the same text in one command, is
there a way to use the Repeat function, or any other "one-liner" method, for
doing it? In other words, to achieve the same effect as the following but
without having to use a counting variable:
for x=1 to 20
selection.find.execute(text)
next x

....using something like this:
Repeat(selection.find.execute(text))(20)

(I've tried a number of different contortions using the Repeat function but
without any success.)
 
D

Doug Robbins - Word MVP

Selection.HomeKey wdStory
With Selection.Find
Do While .Execute(findText:="text", Forward:=True, Wrap:=wdFindStop) =
True
'Do what you want with the "text"
Loop
End With

Whether the Wrap needs to be wdFindStop or wdFindContinue will depend upon
what you are doing. It may also be necessary to have Selection.Collapse
wdCollapseEnd before the Loop command to prevent getting into a never ending
loop. This will also depend upon what you are doing with the found "text".
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
H

Helmut Weber

Hi muyBN,

is it a question of programming style
or is it just a matter of curiosity?

If you have to do a lot of replacements,
always using the same parameters,
you could write your own function or procedure,
taking only a few arguments,
just what you need or want or like.

The most spartanic way could look like this:

x = MyReplace(r, a, b, c)
' -----------------------
r = range, where
a = string, search for
b = string, replace with
c = long, counter

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
M

muyBN

Thanks for your reply. It might be in part for curiosity, but mainly for the
desire to write clean "one-liners" (wish more comedy shows would do that,
ha-ha). I was hoping for something along the lines of the Selection.Move
function with the "Count:=" option.

You wrote "c = long, counter"--I'm assuming that that would be like the
following in concept:

For c=1 to 20
Find [text]
Next c

How would you use a range with this? I think I know but I'm wondering if you
will complete the code. I'm more tied into using Selection.Find since, as I
mentioned in a comment that should be posting soon, I find searching with
ranges to be a lot slower, at least with my bigger projects where I'm
processing through a lot of data.
 
H

Helmut Weber

Hi Bryan,
You wrote "c = long, counter"--I'm assuming that that would be like the
following in concept:

For c=1 to 20
Find [text]
Next c

How would you use a range with this?
I think I know but I'm wondering if you will complete the code.

' ------------------------------------------------------------
Sub MyReplace(r As Range, a As String, b As String, c As Long)
If c = 0 Then Exit Sub
Dim t As Long
Dim w As Range
t = 0
c = c - 1
Set w = r.Duplicate
With w.Find
.Text = a
.Replacement.Text = b
While .Execute(Replace:=wdReplaceOne) _
And t < c And w.InRange(r)
t = t + 1
w.start = w.End
w.End = r.End + 1
Wend
End With
End Sub
' ------------------------------------------------
Sub Test45689()
MyReplace Selection.Range, "x", "y", 10
End Sub
' ------------------------------------------------
I'm more tied into using Selection.Find since, as I
mentioned in a comment that should be posting soon, I find searching with
ranges to be a lot slower, at least with my bigger projects where I'm
processing through a lot of data.

Range is usually much slower in tables than the selection.
Google for my decent name and "ranges", "table".
Factor 50 sometimes.

Also, as far as I see,
there is hardly a difference in speed between
range and selection, if the selection is the whole doc,
or, if the selection is the insertion point at the start of doc.

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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