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

On Tue, 27 Dec 2005 20:33:16 -0500, Jay Freedman
wrote:

Is there some way to define the list of colors as a list, rather than
an array? I am trying to avoid the inconvenience of numbering the
array elements and declaring the array size. Maybe something like:

Colors = wdRed wdGreen wdBlue ...

I am looking for some construct that gets defined without any literals
and the code adapts accordingly.


Yes, there is a way. First, change the declaration (the Dim statement)
for the array to

Dim iColors As Variant

"Variant" is a special data type that can contain almost any other
kind of variable. Specifically, it can also hold an array of values.
(There's a long discussion of this idea in the thread "Max/Min
Functions" in the microsoft.public.word.vba.general newsgroup, started
by Greg Maxey on 12/2/2005.) Then you can assign an array of values to
this variable this way:

iColors = Array(wdRed, wdGreen, wdBlue)

This replaces the lines

iColors(0) = wdRed
iColors(1) = wdGreen
iColors(2) = wdBlue

and you can simply add more values inside the parentheses, separated
by commas. Nothing else in the macro has to change.


Excellent! Thanks.

Here's my latest version. I have some questions at the end.

'======================= sample code =================
Sub RandCharColors()

Dim oChar As Range
Dim myColor As Word.WdColorIndex
Dim vaColors As Variant
Dim iChar As Integer
Dim sOption As String

'Define the colors to be used
vaColors = Array(wdRed, wdGreen, wdBlack)

'Find out if the user wants random or repeating colors
Const sPrompt As String = "Click on:" & vbCrLf & _
"Yes = random colors" & vbCrLf & _
"No = repeating colors"
Const sTitle As String = "Random Character Colors Macro"
sOption = MsgBox(sPrompt, vbYesNoCancel, sTitle)
If sOption vbYes And sOption vbNo Then 'If neither yes or no,
exit
Call MsgBox("No action taken", , sTitle)
Return
End If

'Apply the colors
iChar = 0
Randomize
For Each oChar In Selection.Characters
If oChar.Text = " " Or oChar.Text = vbCr Or oChar.Text = vbLf Then
myColor = vbBlack
ElseIf sOption = vbYes Then
myColor = vaColors(Int((UBound(vaColors) + 1) * Rnd()))
Else
myColor = vaColors(iChar Mod (UBound(vaColors) + 1))
iChar = iChar + 1
End If
oChar.Font.ColorIndex = myColor
Next oChar

End Sub
'===========================================

1. Is there some way to modify the MsgBox function so that it will put
up buttins with different labels? I would like "Random", "Repeating",
and "Cancel". If not, is there another function to accomplish that?

2. I am testing for certain characters, such as space, CR and LF, so I
can skip them. Otherwise, the repeating pattern gets off. I should
probably add others such as tab. Is there a good way to test the
current character against a list (without doing separate compares) or
is there a way to test if it is a printable character (a-z, 0-9, or
certain specials (!#$%...)?

3. What's the best way to call the macro other than assigning it to a
keyboard shortcut?

Thanks. This has been kinda fun.

--
Running Word 2000 SP-3 on Windows 2000