Re: Search all the characters then get range for some of them
- From: leverw <leverw@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 14 Feb 2008 17:43:00 -0800
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.
- Follow-Ups:
- Re: Search all the characters then get range for some of them
- From: Doug Robbins - Word MVP
- Re: Search all the characters then get range for some of them
- References:
- Re: Search all the characters then get range for some of them
- From: Jay Freedman
- Re: Search all the characters then get range for some of them
- Prev by Date: Re: Search all the characters then get range for some of them
- Next by Date: Re: Search all the characters then get range for some of them
- Previous by thread: Re: Search all the characters then get range for some of them
- Next by thread: Re: Search all the characters then get range for some of them
- Index(es):
Relevant Pages
|