Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / June 2007

Tip: Looking for answers? Try searching our database.

using repeat function for search

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
muyBN - 28 May 2007 06:34 GMT
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.)

Signature

Bryan

Doug Robbins - Word MVP - 28 May 2007 08:33 GMT
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".
Signature

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

> 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,
[quoted text clipped - 11 lines]
> but
> without any success.)
Helmut Weber - 28 May 2007 10:18 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

muyBN - 05 Jun 2007 21:20 GMT
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.

Signature

Bryan

> Hi muyBN,
>
[quoted text clipped - 15 lines]
> b = string, replace with
> c = long, counter
Helmut Weber - 05 Jun 2007 23:43 GMT
Hi Bryan,

>You wrote "c = long, counter"--I'm assuming that that would be like the
>following in concept:
[quoted text clipped - 4 lines]
>
>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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.