Calling Find.Execute more than once....

D

Detlef Conradin

I've tried to replace text in a document by calling
selection.find.execute in a loop.
and replacing text by selection.text="some new text"
inside the loop.

Basically something like this:

While Selection.Find.Execute
Selection.Text="some new text"
Wend

Unfortunately this seems no to work. Only the first occurence gets
replaced. If I do not replace anything the selection cycles correctly
trough the document. The problem is probably that the
second time the execute method gets called it searches only
"some new text", which doesn't contain my pattern anymore...
So the loop ends....

But why does it continue if i do not change selection.text in the loop?
 
D

Detlef Conradin

Hi Detlef,
... don't ask me why, but selection as well as range
get newly defined if a search is successfull.
How would you access the text found, if this wouldn't happen?
The whole document is searched, if range or selection
was collapsed or have a certain length (0 or 1 ???).
The strange thing is: If you call the execute moethod multiple times,
without changing the selection it continues in the original selection.
Not regarding some mysteries about that. Hope this helps:
Public Sub Test01()
Selection.WholeStory
Selection.Collapse
With Selection.Find
.Text = "and"
While .Execute
Selection.Text = "dan"
Selection.Collapse direction:=wdCollapseEnd
Wend
End With
End Sub
I will eventually test this.
After I did the original posting here, I found another solution by myself.
If you are using a range, duplicate it and use the duplicated one to
replace the text, then the search continues in the original range.

I'm now using something like this:

Dim range1 As Range
Dim range2 As Range
Set range1 = mydoc.range

While range1.Find.Execute
set range2 = range1.duplicate
range2.Text="some new text"
Wend
Greetings from Bavaria, Germany
Danke und viele Grüsse aus der Schweiz,
Detlef Conradin
 
O

Oci-One Kanubi

Detlef Conradin said:
I've tried to replace text in a document by calling
selection.find.execute in a loop.
and replacing text by selection.text="some new text"
inside the loop.

Basically something like this:

While Selection.Find.Execute
Selection.Text="some new text"
Wend

Detlef, this works for me:

'*******************************************************************************
Private Function PlaceTableData(doc As Document, D() As Datum, _
LoA as integer, HiA as integer)
As Boolean
Dim A As Long, rang As Range
On Error GoTo Handler
PlaceTableData = True
For A = LoA To HiA
Set rang = doc.Content
rang.Find.Execute FindText:=Dat(A).tag,
ReplaceWith:=Dat(A).val, _
MatchWholeWord:=True, MatchCase:=True,
Replace:=wdReplaceAll
Next A
Set rang = Nothing
Exit Function
Handler:
PlaceTableData = False
End Function

D is an array of:

Type Datum
tag As String
val As String
End Type

The tags are embedded in my template Word document, and the vals are
the variable text that changes for each site-specific version of the
report. Originally this function only replaced values in tables, but
I am now using to also replace text anywhere in the doc.Words
collection.

There is another routine that processes tagged values from LoB to HiB
to set Datasheet values for graphs in the report, and a final routine
that processes tagged values from LoC to HiC to replace the text in
TextBox shapes.

-Richard, with Spirit
--
======================================================================
Richard Hopley Winston-Salem, NC, USA
rhopley[at]earthlink[dot]net
Nothing really matters except Bikes, Sex, and Rock'n'Roll
rhopley[at]wfubmc[dot]edu
OK, OK; computer programming for scientific research also matters
======================================================================
 

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