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)
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)