View Single Post
  #24   Report Post  
Posted to microsoft.public.word.newusers
Jay Freedman
 
Posts: n/a
Default Random letter colors?

On Mon, 02 Jan 2006 14:54:11 -0800, LurfysMa
wrote:
On Wed, 28 Dec 2005 16:01:02 -0500, Jay Freedman
wrote:

On Tue, 27 Dec 2005 20:23:26 -0800, LurfysMa
wrote:


....snip...

You can test against a list by replacing

If oChar.Text = " " Or oChar.Text = vbCr Or oChar.Text = vbLf Then

with

For Each oChar In Selection.Characters
If oChar.Text Like "[A-Za-z0-9!#$%*]" Then


Perfect. That was just what I was looking for.

I was able to get the letters and numbers to work, but ran into
trouble with a few of the special characters:

For single quote, I tried "[A-Za-z0-9']" and "[A-Za-z0-9]'". Neither
worked. With the quote ouside the brackets, no characters were
selected. The help mentions special provisions for []?#*, but not for
the quotes.

For double quote, similar results. I tried "[A-Za-z0-9""]" and
"[A-Za-z0-9]""".

The help warns about "*?#" and the brackets, but this string worked
just fine: "[A-Za-z0-9[*?#]". I could not find a way to make "]" work,
however. I tried "[A-Za-z0-9]]".


Does your document have curly quotes (a.k.a. "smart quotes")
substituted for straight quotes by the AutoFormat As You Type feature?
The Like operator treats them separately.

The single quote that you can type directly into VBA code matches only
straight single quotes in the text. The curly opening and closing
single quotes are Chr$(145) and Chr$(146), respectively. The curly
double quotes are 147 and 148. All four of them would have to be
included in the pattern string for the Like operator to match them.

Let's start by including just the straight single and double quotes in
a pattern:

If oChar.Text Like "[A-Za-z0-9'""]" Then

The single quote should work fine by itself. The double quote would
prematurely end the string if you tried to stick one inside the square
brackets, but two of them together are understood to mean an actual
double quote character instead of the string-ending quote.

Now, to get the curly quotes into the expression, you have to somehow
get them inside the square bracket. You could just jam all the Chr$()
calls in, using the concatenation (&) operator, but I'd make it more
readable by creating a separate string to hold them:

Dim curlies As String
curlies = Chr$(145) & Chr$(146) & Chr$(147) & Chr$(148)
For Each oChar In Selection.Characters
If oChar.Text Like "[A-Za-z0-9'""" & curlies & "]" Then

After the 0-9 the characters there are a single quote, two double
quotes to make a double-quote character, and another double quote to
end that part of the string; then the & curlies & ; and finally the
closing square bracket. As far as the Like operator knows, it's all
one string within square brackets.

--
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.