Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.newusers
Doug Robbins - Word MVP
 
Posts: n/a
Default Random letter colors?

Use \]

See the article "Finding and replacing characters using wildcards" at:

http://www.word.mvps.org/FAQs/Genera...gWildcards.htm


--
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 Mon, 02 Jan 2006 19:45:41 -0500, Jay Freedman
wrote:

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.


Yes, I should have thought of that.

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.


All that now works properly.

The only character I cannot get to work in "]".

I tried doubling it "[A-Z]]]", but that didn't work.

The help says it cannot be used within a group, but it can be used
outside of a group. There is no example and I can't figure that one
out.



I also got the user form to work. I'll post the complete macro when I
get "]" working.

--
Running Word 2000 SP-3 on Windows 2000



  #2   Report Post  
Posted to microsoft.public.word.newusers
LurfysMa
 
Posts: n/a
Default Random letter colors?

On Tue, 3 Jan 2006 06:23:40 +0100, "Doug Robbins - Word MVP"
wrote:

Use \]

See the article "Finding and replacing characters using wildcards" at:

http://www.word.mvps.org/FAQs/Genera...gWildcards.htm


Do you mean like this: "[A-Za-z0-9\]]" ?

The following code:

If obChar.Text Like "[A-Za-z0-9\]]" then...

is always false with even for A-Z, a-z, and 0-9.

Change it to:

If obChar.Text Like "[A-Za-z0-9]" then ...

and it works perfectly.


--
Running Word 2000 SP-3 on Windows 2000
  #3   Report Post  
Posted to microsoft.public.word.newusers
Graham Mayor
 
Posts: n/a
Default Random letter colors?

Or the revised version of it at
http://www.gmayor.com/replace_using_wildcards.htm

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org


Doug Robbins - Word MVP wrote:
Use \]

See the article "Finding and replacing characters using wildcards" at:

http://www.word.mvps.org/FAQs/Genera...gWildcards.htm



"LurfysMa" wrote in message
...
On Mon, 02 Jan 2006 19:45:41 -0500, Jay Freedman
wrote:

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.


Yes, I should have thought of that.

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.


All that now works properly.

The only character I cannot get to work in "]".

I tried doubling it "[A-Z]]]", but that didn't work.

The help says it cannot be used within a group, but it can be used
outside of a group. There is no example and I can't figure that one
out.



I also got the user form to work. I'll post the complete macro when I
get "]" working.

--
Running Word 2000 SP-3 on Windows 2000



  #4   Report Post  
Posted to microsoft.public.word.newusers
LurfysMa
 
Posts: n/a
Default Random letter colors?

On Tue, 3 Jan 2006 08:47:47 +0200, "Graham Mayor"
wrote:

Or the revised version of it at
http://www.gmayor.com/replace_using_wildcards.htm

--

Graham Mayor - Word MVP


OK. I have read both articles. Based on that, it seems to me that the
expression "[A-Za-z0-9[\]]" ought to find all of the letters and
numbers plus the square brackets ([]) in a Like comparison.

But it doesn't match any characters at all.

However, if I delete the "\]", leaving "[A-Za-z0-9[]", then it will
match all of the letters and numbers plus "[".

What am I doing wrong?

--
Running Word 2000 SP-3 on Windows 2000
  #5   Report Post  
Posted to microsoft.public.word.newusers
Graham Mayor
 
Posts: n/a
Default Random letter colors?

It certainly works when used from a Wildcard search within Word - As for
your code, that has long since lapsed from my newsreader.

--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org


LurfysMa wrote:
On Tue, 3 Jan 2006 08:47:47 +0200, "Graham Mayor"
wrote:

Or the revised version of it at
http://www.gmayor.com/replace_using_wildcards.htm

--

Graham Mayor - Word MVP


OK. I have read both articles. Based on that, it seems to me that the
expression "[A-Za-z0-9[\]]" ought to find all of the letters and
numbers plus the square brackets ([]) in a Like comparison.

But it doesn't match any characters at all.

However, if I delete the "\]", leaving "[A-Za-z0-9[]", then it will
match all of the letters and numbers plus "[".

What am I doing wrong?





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

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

--
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 08:47:47 +0200, "Graham Mayor"
wrote:

Or the revised version of it at
http://www.gmayor.com/replace_using_wildcards.htm

--

Graham Mayor - Word MVP


OK. I have read both articles. Based on that, it seems to me that the
expression "[A-Za-z0-9[\]]" ought to find all of the letters and
numbers plus the square brackets ([]) in a Like comparison.

But it doesn't match any characters at all.

However, if I delete the "\]", leaving "[A-Za-z0-9[]", then it will
match all of the letters and numbers plus "[".

What am I doing wrong?

--
Running Word 2000 SP-3 on Windows 2000



  #7   Report Post  
Posted to microsoft.public.word.newusers
LurfysMa
 
Posts: n/a
Default Random letter colors?

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

  #9   Report Post  
Posted to microsoft.public.word.newusers
LurfysMa
 
Posts: n/a
Default Random letter colors?

On Tue, 03 Jan 2006 10:37:25 -0500, Jay Freedman
wrote:

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


I discovered a way to get all of the letters, numbers, and special
characters, including the curly quotes, with a fairly simply pattern
for use in the Like operator and without needing two compares.

The trick lies in realizing that all of these characters are in order
starting with the space character (decimal 32, hex 20) and ending with
the ~ (decimal 126, hex 7E). I got this information from

http://www.lookuptables.com/

This enables a single range to get everything including all of the
wild card characters, the straight quotes, and the brackets.

"[ -~]"

The code would look something like this:

For Each obChar In Selection.Characters
If obChar.Text Like "[ -~]" Then
obChar.Font.ColorIndex =GetNextColor
End If
Next obChar

To also get the curly quotes (145-149), simply add another range iont
the same group:

"[ -~" & Chr$(145) & "-" & Chr$(148) & "]"

Slick, if I do say so myself.

OK, now someone tell me why this won't work! ;-)

--
Running Word 2000 SP-3 on Windows 2000
  #10   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





Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert to Symbols Omar Menjivar Microsoft Word Help 7 June 27th 05 07:32 PM
School Reports CS Mailmerge 5 May 4th 05 07:20 AM
Letter Wizard templates will not work e2rodney Mailmerge 1 February 17th 05 08:30 PM
Can't save merged form letter... Raven Mailmerge 1 January 27th 05 08:34 PM
How do I access a created template from letter wizard trying to streamline Microsoft Word Help 6 January 5th 05 02:52 AM


All times are GMT +1. The time now is 07:14 AM.

Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"