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



I have only been doing this VSTO programming for the last 4 weeks. So how do
I "... load the words into an array" quickly as you describe? This is
exactly what I need to do. But when I call Document.Range, it gives me all
the word ranges in the document range, but I have to go through each
Word.Range call to get the actual word, which is not efficient to me, right?

Many thanks in advance again!

"Doug Robbins - Word MVP" wrote:

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
    ... we need to search and replace (actually just gray out the text). ... formatting of the .Replacement property and executing with the wdReplaceAll ... 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: 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 ... "Jay Freedman" wrote: ... characters and/or their formatting, you can do that by specifying the ... Dim oRg As Range ...
    (microsoft.public.word.vba.general)
  • Re: Good Docvariable information source
    ... Perhaps you could use the \*CharFormat switch in the DocVariables fields to ... Variable object is a simple String data type. ... contain an array or any formatting information. ...
    (microsoft.public.word.vba.general)
  • Re: SUMPRODUCT formula using hours:minutes
    ... It is because they are wrapped inside SUMPRODUCT. ... SUMPRODUCT the way used by you is an array formula without the need for ... previous threads, copied blank cells and pasted the formatting, etc. ...
    (microsoft.public.excel.misc)