Find/Replace using the Word Range object

M

Minerva Jo

I have a table in a Word document (Word XP, VB.NET). The
following logic finds a string and then determines whether
to use the find/replace object or whether to delete the
row this text string is in.

I have a test document that I know has a three row table.
It is part of the oWordRange object.

With oWordRange.Find
.ClearFormatting()

Do While .Execute(FindText:="[#myString#]", _
MatchCase:=False, _
MatchWholeWord:=False) = True

iCounter += 1

If iCounter > iInfoTags Then
'delete this row
oWordRange.Rows.Delete()
Else

'just replace the text
oWordRange.Find.Execute(FindText:="[#myString#]", _
ReplaceWith:=strReplaceString, _
Replace:=Word.WdReplace.wdReplaceAll, _
Forward:=True)
End If

Loop
End With

If I have the Word doc visible as I step thru this code,
it does the following:

1. Do statement returns a True. It finds the *2nd* text
string and replaces it

2. Do statement returns a True. It finds the *1st* text
string and replaces it (can't understand why it's doing it
in this order)

3. Do statement returns a True. It finds the 3rd text
string but when the "oWordRange.Find.Execute" statement
runs, it does NOT do the find/replace. The text string is
still there.

4. Do statement returns a True (fourth time thru it should
be false but since it didnt find/replace correctly, we
step into the loop a 4th time). It then runs the "delete
row" logic. I dont want the row deleted but since the
iCounter value is over the limit, it deletes the row.

As an experiment, I commented out the "delete row"
statement and ran the find/replace code in its place. What
happens is, you never exit the "Do while x = True" loop
because that last text string is replaced (even tho the Do
Loop returns a True, so it IS found!!). I am incredibly
puzzled. Appreciate any ideas.

(note: iInfoTags = a parameter passed in that is calculated
elsewhere)
 
C

Cindy M -WordMVP-

Hi Minerva,

When you use .Execute in the Do While line, you're actually
executing Find. Then you execute again. That explains why it
locates and replaces the second instance of the string the
first time you run through. Instead of doing this, declare a
variable of the Boolean type and test it in the Loop line
(Loop Until Not bFound for example). bFound = .Found, then,
AFTER you've done the execution within the loop.

The "idiosyncracy" you're running into after this point has to
do how Word deals with ranges in relation to tables.
1. When Find is successful, the oWordRange object is set to
the found text. The range changes. So the next time Find runs,
it runs from this point, forwards.

2. Now add this to the fact that, when that Range is located
in a table, the start of the range gets set to the start of
the table. That explains why the next "hit" is then the first
instance of the string you're searching. And so is the
following one, until it's replaced.

I think, if you make the change to check the Found property,
rather than having .Execute in your loop twice, this
particular problem should clear up. But you need to have the
rest of the information above if you do other complex Find
work in Word.
I have a test document that I know has a three row table.
It is part of the oWordRange object.

With oWordRange.Find
.ClearFormatting()

Do While .Execute(FindText:="[#myString#]", _
MatchCase:=False, _
MatchWholeWord:=False) = True

iCounter += 1

If iCounter > iInfoTags Then
'delete this row
oWordRange.Rows.Delete()
Else

'just replace the text
oWordRange.Find.Execute(FindText:="[#myString#]", _
ReplaceWith:=strReplaceString, _
Replace:=Word.WdReplace.wdReplaceAll, _
Forward:=True)
End If

Loop
End With

If I have the Word doc visible as I step thru this code,
it does the following:

1. Do statement returns a True. It finds the *2nd* text
string and replaces it

2. Do statement returns a True. It finds the *1st* text
string and replaces it (can't understand why it's doing it
in this order)

3. Do statement returns a True. It finds the 3rd text
string but when the "oWordRange.Find.Execute" statement
runs, it does NOT do the find/replace. The text string is
still there.

4. Do statement returns a True (fourth time thru it should
be false but since it didnt find/replace correctly, we
step into the loop a 4th time). It then runs the "delete
row" logic. I dont want the row deleted but since the
iCounter value is over the limit, it deletes the row.

As an experiment, I commented out the "delete row"
statement and ran the find/replace code in its place. What
happens is, you never exit the "Do while x = True" loop
because that last text string is replaced (even tho the Do
Loop returns a True, so it IS found!!). I am incredibly
puzzled. Appreciate any ideas.

(note: iInfoTags = a parameter passed in that is calculated
elsewhere)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep
30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 

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