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



It's a simple matter to load the words into an array and then use code
similar to Jays to interate through the array, and process each word in
turn.

You probably would not have time to get a cup of coffee while it was doing
it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"leverw" <leverw@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:8373C72E-2042-4897-8B61-0CAACC9543F7@xxxxxxxxxxxxxxxx
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: Does "Close" save changes?
    ... Thank you, Jay. ... Because a string variable can't hold any kind of formatting, ... > Dim oRg As Range ... > Set doc = Documents.Open ...
    (microsoft.public.word.vba.beginners)
  • Re: Printing list of styles in my document
    ... Jay & Suzanne, ... The macro worked fine. ... styles and formatting I am trying to track down are scattered far apart. ...
    (microsoft.public.word.docmanagement)
  • Re: Search all the characters then get range for some of them
    ... OK, Jay and Doug, I mis-understood your solutions. ... to know the positions of each word in a document so we can gray it out later ... array, but loading an array with the list of words that you need to search ... Dim WordsToFind As Variant ...
    (microsoft.public.word.vba.general)
  • Re: bold/italic formatting in userform textbox
    ... Thanks, Steve, that's a great help. ... >Hi Jay and Katherine, ... >> can set the formatting at design time and you won't need any code. ... >> the textbox, go to the Properties pane, and click the Font item. ...
    (microsoft.public.word.vba.general)