Search for font color

D

dsc

Best I can figure, the following code ought to search my document for the
terms in SearchArray and turn them green. However, it also turns other
sections of text green, apparently at random. Can anybody tell me why that
might be?

Thanks in advance.

For i = 0 To UBound(SearchArray)

TermString = SearchArray(i, 0)
StandardString = SearchArray(i, 1)

'Insert all English entries for this client
With ActiveDocument.Content.Find
.Font.Color = wdColorBlack
.Text = TermString
.Replacement.Font.Color = wdColorGreen
.Replacement.Text = TermString
.Forward = True
.wrap = wdFindContinue
.Format = True
.MatchCase = True
.MatchWholeWord = True
.MatchByte = True
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
.Execute Replace:=wdReplaceAll
End With
Next
 
J

Jay Freedman

I'm not certain this is the fix, as I haven't tried to reproduce your
problem and in any event don't have your document, but it's worth the
attempt...

At the top of your macro, declare a Range object and set it to the
document's range. Then use the Find property of the Range object
instead of ActiveDocument.Content.Find:

Dim myRange As Range
Set myRange = ActiveDocument.Range

...

With myRange.Find

The reason this might have an effect is a bit obscure. When you call
the .Execute method of a Range object, if the item is found then the
Range is redefined to cover only the found instance. But
ActiveDocument.Content by definition covers the entire document and
can't be redefined. This can play havoc with macros that try to treat
it as an ordinary Range object. I've never understood why VBA doesn't
flag this as an error.
 
D

dsc

Jay,

Thanks for the reply.
Dim myRange As Range
Set myRange = ActiveDocument.Range
With myRange.Find

No, that doesn't work.

The problem is somewhere in the code for searching. When I comment out the
line:

Everything works fine. The problem is that I need that line to make the
macro work. See, what I do is go through and turn one set of terms green,
then go through again and turn another set red. However, since there's
overlap between the two sets of terms, I need to exclude the green terms
from the second search.

I really need some help with this one. I've been struggling with it for a
long time, and it seems to be entirely behond my meager powers.
 
D

Doug Robbins - Word MVP

Use the following:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = TermString
.Font.Color = wdColorAutomatic
.Replacement.Text = TermString
.Replacement.Font.Color = wdColorGreen
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

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