Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find & replace text for only selected area of document

Status
Not open for further replies.

AMJZ

Technical User
Nov 17, 2004
6
GB
I need to recolour text for a selected portion of a word document but omitting the recolor if the text is found in a table. Here's what I have so far:

With workrange.Find
.ClearFormatting
.Text = "SearchText"
Do While .Execute
If Not workrange.Information(wdWithInTable) Then
workrange.Font.Color = RGB(255, 0, 0)
End If
Loop
End With

The recolouring starts at the beginning of my selected area and text in tables is ignored. All OK. But the problem is that the loop continues beyond the end of my original selected area to the end of the document, recoloring every subsequent instance of the text string not in a table.

I've probably missed something simple - what is it?
 
Hi AMJZ,

The problem here is that the Find.execute redefines the range. You need to find some way of determining whether you are still withing the original range. One way would be to remember the end of it and check inside the loop ..

Code:
[blue][red]RangeEnd = workrange.End[/red]
With workrange.Find
.ClearFormatting
.Text = "SearchText"
Do While .Execute
[red]If workrange.Start > RangeEnd Then Exit Do[/red]
If Not workrange.Information(wdWithInTable) Then
workrange.Font.Color = RGB(255, 0, 0)
End If
Loop
End With[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
Thanks Tone. Works a treat.
You truly are a very very nice man - AMJZ.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top