Re: Search all the characters then get range for some of them



Thanks for your answer. But in our case, we have many words (>> 1000) that
we need to search and replace (actually just gray out the text). Doing it
one at a time is not very scalable, right? I thought I could get the entire
text and look for them myself. If some are positioned consecutively, I can
gray out several words at a time.

Thanks again.

"Jay Freedman" wrote:

On Thu, 14 Feb 2008 14:30:05 -0800, leverw <leverw@xxxxxxxxxxxxxxxxxxxxxxxxx>
wrote:

How do I programmatially search all the characters in a word document, then
get Range for some of them so I can change font, etc? I hate to call
Document.Characters because it will take forever. But if I call
Document.Range(ref 1, ref missing) to get all the text, then how do I know
the positions of some of the text since the document may contain
non-printable characters. For instance, I want to search for "Hello world"
in a document, set the text font color to red. Any help will be greatly
appreciated.

Do not attempt to get a range that way. Use the .Find method of a Range object
that is initialized to the document's range. If all you're doing is changing
characters and/or their formatting, you can do that by specifying the text or
formatting of the .Replacement property and executing with the wdReplaceAll
parameter. If you need to run more sophisticated logic on the found text, you
can use the Range object itself: When the .Find.Execute succeeds in finding the
search term, the Range object is automatically redefined to cover only the found
text.

As an example of the first scheme, start with a document in which there are
several occurrences of "Hello world" scattered about. Then run this macro to
change the color of all of the occurrences to red:

Sub demo1()
Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "hello world"
.Replacement.Text = "^&" ' same as found
.Replacement.Font.Color = wdColorRed

.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWildcards = False

.Execute Replace:=wdReplaceAll
End With
End Sub

As an example of the second scheme, start with a document in which there are
several occurrences of red text scattered about. Then run this macro to change
the color of that text to blue only if the red text includes the word "blue":

Sub demo2()
Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Text = ""
.Font.Color = wdColorRed

.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWildcards = False

Do While .Execute
If InStr(LCase(oRg.Text), "blue") > 0 Then
oRg.Font.Color = wdColorBlue
End If
oRg.Collapse wdCollapseEnd
Loop
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.

.



Relevant Pages

  • Re: Search all the characters then get range for some of them
    ... load the words into an array" quickly as you describe? ... characters and/or their formatting, you can do that by specifying the ... Dim oRg As Range ... oRg.Font.Color = wdColorBlue ...
    (microsoft.public.word.vba.general)
  • Re: Search all the characters then get range for some of them
    ... get Range for some of them so I can change font, ... formatting of the .Replacement property and executing with the wdReplaceAll ... Dim oRg As Range ... oRg.Font.Color = wdColorBlue ...
    (microsoft.public.word.vba.general)
  • RE: Visio objects turn gray after release
    ... I suspect that things will turn gray in both cases. ... Copy your drawing, open a new file, and paste your drawing? ... properties it shows it does have the same formatting as the pervious. ... tried to copy and paste existing shapes? ...
    (microsoft.public.visio)
  • Re: change text color of bullet item
    ... nor manual formatting carries into the TOC. ... bulleted item or heading and changed the font the bulleted entry would change ... Also in a table of contents I see if I strike thrue or gray ...
    (microsoft.public.word.docmanagement)
  • Re: Conditional Formatting using blocks of rows
    ... If white is the default fill color you only need to test for gray and blue. ... Select the range of cells in question. ... Goto the menu Format>Conditional Formatting ... rows 6-13 8 data rows ...
    (microsoft.public.excel.worksheet.functions)