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

It looks like I went to bed too early last night. :-)

The implication of the too-terse help topic is that you can't combine
the right bracket with anything else in a pattern that includes a
group. You do have to do two separate comparisons, although they can
be in the same statement:

If (obChar.Text Like "[A-Za-z0-9?[*#]") Or _
(obChar.Text Like "]") Then

or the equivalent

If (obChar.Text Like "[A-Za-z0-9?[*#]") Or _
(obChar.Text = "]") Then

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

On Tue, 03 Jan 2006 03:28:45 -0800, LurfysMa
wrote:

On Tue, 3 Jan 2006 11:32:49 +0100, "Doug Robbins - Word MVP"
wrote:

It turns out that you are doing nothing wrong (apart from not making use of
the Help file g). It is a difference between the use of a Wildcard Find
and the use of the Like function.

The following is from the Visual Basic Help file:

Quote
Note To match the special characters left bracket ([), question mark (?),
number sign (#), and asterisk (*), enclose them in brackets. The right
bracket (]) can't be used within a group to match itself, but it can be used
outside a group as an individual character.

Unquote


I saw that in the help file. I've read it 50 times. I still can't make
it work. If it's so obvious to you, why don't you just provide the
solution -- the exact compare string?

Here's my code.

For Each obChar In Selection.Characters
If obChar.Text Like "[A-Za-z0-9]" Then
ilColorNext = vbRed
Else
ilColorNext = vbBlack
End If
Next obChar

That code turns all of the letters and numbers red and everything else
black.

Here are some of the other strings I have tried in place of the one
above:

1. This string turns all of the numbers and letters plus a few special
characters red. This works for all special characters I tried except
"]". It even works for dash ("-") provided that it is the last
character (or the first).

"[A-Za-z0-9?[*#]"

2. But it does not work for "]". This string doesn't turn anything
red:

"[A-Z]]"

3. It was not clear to me whether the extra "]" in the string above
was inside or outside the group, so I tried putting in first, but it
turns everything black:

"][A-Z]"

4. Just to make sure I got it outside the group, I making it the only
thing in the string. This string turns all ]'s red and everuything
else black. But how can I code it in a string with anything else?

"]"

5. Someone said to use a back slash, so I tried it. All of these
strings turn everything black:

"[A-z\]]"
"\][A-z]"
"[A-z]\]"

I give up. If you know a string that will work, how about putting me
out of my misery and just posting it.

I did come up with a workaround. Just make two comparisons: one for
just the "]" and one for everything else.