Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2004

Tip: Looking for answers? Try searching our database.

Search for font color

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dsc - 25 Dec 2004 11:16 GMT
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
Jay Freedman - 25 Dec 2004 16:34 GMT
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.

--
Regards,
Jay Freedman
Microsoft Word MVP         FAQ: http://word.mvps.org

>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
[quoted text clipped - 27 lines]
>            End With
>Next
dsc - 26 Dec 2004 03:20 GMT
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:

>>     .Font.Color = wdColorBlack

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.
Doug Robbins - Word MVP - 26 Dec 2004 04:45 GMT
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

Signature

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

> Jay,
>
[quoted text clipped - 19 lines]
> 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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.