View Single Post
  #34   Report Post  
Posted to microsoft.public.word.newusers
Doug Robbins - Word MVP
 
Posts: n/a
Default Random letter colors?

You must have mis-read this part

'The right bracket (]) can't be used within a group to match itself...'

That is the left bracket ([) CAN be used, but the right bracket (]) CANNOT
be used within a group.

The following code acts on the ] plus any characters Like "[A-Za-z0-9]",
formatting them as Red, formatting everything else in the selection as Black
(I started with everything yellow)

Dim obChar As Range
For Each obChar In Selection.Characters
If obChar.Text Like "[A-Za-z0-9]" Then
obChar.Font.Color = wdColorRed
ElseIf obChar.Text Like "]" Then
obChar.Font.Color = wdColorRed
Else
obChar.Font.Color = wdColorBlack
End If
Next obChar

That was not "so obvious" to me, I first tried to use

Dim obChar As Range
For Each obChar In Selection.Characters
If obChar.Text Like "[A-Za-z0-9](])" Then
obChar.Font.Color = wdColorRed
Else
obChar.Font.Color = wdColorBlack
End If
Next obChar

but that just turned everything black, so I then just included the ElseIf to
pick up the ].

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

"LurfysMa" wrote in message
...
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.

--
Running Word 2000 SP-3 on Windows 2000