Longest string of words of one color

J

Joe

I can't get my mind around constructing a function to find the longest
string of black/default words in a document. About half of the words
in the doc are red or blue or green.
Not sure how to handle words that are half black and half red.
The characters are in Unicode Greek, not roman characters.

Ideas?
 
K

Karl E. Peterson

After serious thinking Joe wrote :
I can't get my mind around constructing a function to find the longest
string of black/default words in a document. About half of the words
in the doc are red or blue or green.
Not sure how to handle words that are half black and half red.
The characters are in Unicode Greek, not roman characters.

Ideas?

Sounds like a classic parsing exercise. You simply iterate through the
character buffer one at a time, noting the characteristic of interest.
Or, more importantly, when the characteristic changes. In other words,
what you /really/ care about are when the text transitions from one
color to another. This is essentially the same exercise as splitting a
string into words, using spaces as delimiters. There are lots of
algorithms out there for that, so those could prove a good starting
point for learning about character buffer parsing.
 
K

Klaus Linke

Joe said:
I can't get my mind around constructing a function to find the longest
string of black/default words in a document. About half of the words
in the doc are red or blue or green.
Not sure how to handle words that are half black and half red.
The characters are in Unicode Greek, not roman characters.

Ideas?

Use a wildcard search for <*> and the font color.
< is the wildcard that matches the start of a word, > matches the end, and *
matches any text.

To get the number of matches unfortunately isn't too easy.
You can use the trick from here:
http://word.mvps.org/FAQs/MacrosVBA/NoTimesTextInDoc.htm

Or faster, replace with some character that doesn't appear (say one from a
"private use" area like Unicode U+4000) plus the found text
(.Replacement.Text = ChrW(&H4000) & "^&"), then note how much longer the
text is than before, then undo the replacement with ActiveDocument.Undo

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top