Puzzled about the Range object

L

LaVerne

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.

'iInfoTags = a parameter passed in that is calculated
elsewhere
 
L

LaVerne

I typed this wrong and need to fix it...

What happens is, you never exit the "Do while x = True"
loop because that last text string is *NOT* replaced (even
tho the Do Loop returns a True, so it IS found!!).

I am still INCREDIBLY, mind bogglingly puzzled.
 
J

Jay Freedman

Hi, LaVerne,

The problem occurs because a successful Find.Execute moves the range to the
found text. That means that further searches aren't based on the range you
started with.

The first Find (in the Do statement) makes oWordRange cover only the first
occurrence of the string. The second one (in the Else clause) then goes to
the second occurrence and replaces it. Things just get worse from there. :-(

I think you need to make these changes:

1. Remove the oWordRange.Find.Execute from the Else clause
and instead put in
oWordRange.Text = strReplaceString
Because the Find leaves oWordRange covering just the found
string, this is the same as replacing.

2. Between the End If and the Loop, insert
oWordRange.Collapse _
Direction:=Word.WdCollapseDirection.wdCollapseEnd
so the Find doesn't continue to find the same occurrence.

3. In the Find in the Do statement, add the parameter
Wrap:=Word.WdFindWrap.wdFindStop
so a Find at the end of the document doesn't wrap around to
the beginning.

These changes haven't been tested -- I'll leave that to you.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://www.mvps.org/word
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.

'iInfoTags = a parameter passed in that is calculated
elsewhere
 
T

The Big L

wowzers. thank you for giving me something to go on. i
appreciate your input
-laverne dafazio
-----Original Message-----
Hi, LaVerne,

The problem occurs because a successful Find.Execute moves the range to the
found text. That means that further searches aren't based on the range you
started with.

The first Find (in the Do statement) makes oWordRange cover only the first
occurrence of the string. The second one (in the Else clause) then goes to
the second occurrence and replaces it. Things just get worse from there. :-(

I think you need to make these changes:

1. Remove the oWordRange.Find.Execute from the Else clause
and instead put in
oWordRange.Text = strReplaceString
Because the Find leaves oWordRange covering just the found
string, this is the same as replacing.

2. Between the End If and the Loop, insert
oWordRange.Collapse _
Direction:=Word.WdCollapseDirection.wdCollapseEnd
so the Find doesn't continue to find the same occurrence.

3. In the Find in the Do statement, add the parameter
Wrap:=Word.WdFindWrap.wdFindStop
so a Find at the end of the document doesn't wrap around to
the beginning.

These changes haven't been tested -- I'll leave that to you.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://www.mvps.org/word
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.

'iInfoTags = a parameter passed in that is calculated
elsewhere


.
 

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