Home |
Search |
Today's Posts |
|
#1
Posted to microsoft.public.word.newusers
|
|||
|
|||
Random letter colors?
On Tue, 27 Dec 2005 08:35:22 +1300, "Peter in New Zealand"
peterbalplugATxtraSPOTcoSPOTnz wrote: Every year I send out a Christmas newsletter to quite a lot of children (grownup now, and grand children. I have established this as a family tradition over several years, and they all seem to enjoy their Christmas letter from Grandad. I use Word to compose it, with text and pictures, and it's always been a heap of fun. Each sub heading has always been prepared with alternative red and green letters, and looks real great in that context. It's just for the kids, and only once a year, but something to do it automatically would be a great labour saver to say the least. Peter, Way back in 2005, I asked how write a macro to automatically change the colors of individual letters in some text. You asked for a copy of the macro. It took me awhile to write it, and then I forgot that you had asked. I just came across your post, so here's the macro. I was going to have it allow the user to select the colors, but I never got around to that. It's set for half red and half green. If you want some other mix, you will need to edit the vaColors variable. Maybe someone can suggest a way to allow the user to enter the colors (eg, red, red, green). Enjoy... Option Explicit '================================================= ======================================== ' Macro: MyRandomCharColors ' ' Keyboard Shortcut: None ' ' Set each character in the selection to a different color ' 12/23/05 Basic macro posted to microsoft.public.word.newusers by Jay Freedman, MVP ' He then continued to help me refine it. ' ' To Do: ' * Limit maximum consecutive in random order '================================================= ======================================== Sub MyRandCharColors() Const svTitle As String = "Random Character Colors Macro" 'Title for MsgBox's etc. Dim obChar As Range 'Object variable Dim ilColorNext As Word.WdColorIndex 'Color Index property (long) Dim ilColorLast As Long 'The last color that was applied Dim vaColors As Variant 'Variant array to hold colors Dim ilChar As Long 'Color index, if repeating; counter if random Dim svCharList As String 'The list of characters that will be colored Dim ilMaxChar As Long 'Max consecutive characters of the same color (random or repeating) Dim obForm As frmCharColors 'Object variable? Set obForm = New frmCharColors 'Set up an instance? ilMaxChar = 2 'Set the upper limit for consecutive characters of the same color (2 is good) 'Define the list of colors to be used. Colors can be included more than once. vaColors = Array(wdRed, wdGreen, wdBlack) '3 colors vaColors = Array(wdRed, wdGreen, wdBlue) vaColors = Array(wdRed, wdRed, wdGreen) 'Red:green = 2:1 vaColors = Array(wdRed, wdGreen) 'Christmas colors 'Define which characters will be colored. All others will be skipped (colored black). 'This range includes all of the letters, numbers, and specials below Ascii 127. 'They all lie between the space (hex 20) and the ~ (hex 7E): svCharList = "[ -~]" 'If we want to exclude the space, we need to start at the next character, but that 'is the "!" which is the exclusion character, so we need to start with the next 'character, the ", and add the "!" at the end: svCharList = "[""-~!]" 'The curly (smart) quotes are at Ascii 145-149. We can either build a range using the 'chr$() function: svCharList = "[""-~!" & Chr$(145) & "-" & Chr$(148) & "]" 'or paste the characters from Word or the Immediate window. svCharList = "[""-~!‘-”]" 'Put up a userform to find out if the user wants random or repeating colors obForm.Tag = "Cancel" 'Set it to cancel by default obForm.Show 'Put up the form and get the selection into me.tag If obForm.Tag "Random" And obForm.Tag "Repeat" Then GoTo ExitSub 'If not a choice, quit If obForm.txtRandMax "" Then ilMaxChar = obForm.txtRandMax End If 'Apply the colors ilChar = 0 'Start with the 1st color or zero the counter ilColorLast = -1 'Initialize to a color that can't match the next one Randomize 'Just in case they select the Random option For Each obChar In Selection.Characters 'First, check if it's a character to be colored. Then figure out how (random or repeating). If obChar.Text Like svCharList Then 'If it's a character to be colored, Select Case obForm.Tag 'Use whichever method the user choose Case "Repeat" 'If they chose 'repeat', ilColorNext = MyRandCharColorsRepeat(ilChar, vaColors) Case "Random" 'If they chose 'random', ilColorNext = MyRandCharColorsRandom(ilChar, vaColors, ilMaxChar, ilColorLast) End Select ilColorLast = ilColorNext 'Save the color to check against next character Else 'If it is, color it as requested ilColorNext = vbBlack 'Make it black End If obChar.Font.ColorIndex = ilColorNext Next obChar ExitSub: 'We're done. Unload the form and exit Unload obForm Set obForm = Nothing End Sub 'Called by MyRandCharColorsRepeat 'Select a random color from the list up to the consecutive limit Function MyRandCharColorsRandom(ByRef ilChar As Long, ByVal vaColors As Variant, _ ByVal ilMaxChar As Long, ByVal ilColorLast As Long) As Long Dim nsRnd As Single 'Random color index Do 'Select a random color up to the consecutive limit nsRnd = Rnd() MyRandCharColorsRandom = vaColors(Int((UBound(vaColors) + 1) * nsRnd)) 'Select a random color If MyRandCharColorsRandom ilColorLast Then 'If it's a new color, use it ilChar = 1 'Reset the counter & go Exit Do Else 'If it's the same color, ilChar = ilChar + 1 'Count it If ilChar = ilMaxChar Then Exit Do 'If not too many, go use it End If 'Else, go get another color Loop End Function 'Called by MyRandCharColorsRepeat 'Select the next color in the list, wrapping around to the start Function MyRandCharColorsRepeat(ByRef ilChar As Long, ByVal vaColors As Variant) As Long MyRandCharColorsRepeat = vaColors(ilChar Mod (UBound(vaColors) + 1)) ilChar = ilChar + 1 End Function -- Running Word 2000 SP-3 on Windows 2000 |
#2
Posted to microsoft.public.word.newusers
|
|||
|
|||
Random letter colors?
LurfysMa wrote:
On Tue, 27 Dec 2005 08:35:22 +1300, "Peter in New Zealand" peterbalplugATxtraSPOTcoSPOTnz wrote: Every year I send out a Christmas newsletter to quite a lot of children (grownup now, and grand children. I have established this as a family tradition over several years, and they all seem to enjoy their Christmas letter from Grandad. I use Word to compose it, with text and pictures, and it's always been a heap of fun. Each sub heading has always been prepared with alternative red and green letters, and looks real great in that context. It's just for the kids, and only once a year, but something to do it automatically would be a great labour saver to say the least. Peter, Way back in 2005, I asked how write a macro to automatically change the colors of individual letters in some text. You asked for a copy of the macro. Well, hello there! Voice from the past and all that. I got a copy back then, and I have used it every Christmas since. I honestly cannot remember where it came from, but it works great, so if you were the kind helper back then you have my grateful thanks, along with all my kids and grandkids. Just before last Christmas I sent out my family and friends Christmas newsletter to a much wider audience than previously , and over 70 copies were printed and sent, all with Christmas coloured text courtesy of the macro. Thank you again. -- Peter in New Zealand. (Pull the plug out to reply.) Collector of old cameras, tropical fish fancier, good coffee nutter, and compulsive computer fiddler. |
#3
Posted to microsoft.public.word.newusers
|
|||
|
|||
Random letter colors?
On Fri, 23 Mar 2007 16:28:23 +1200, Peter in New Zealand
wrote: LurfysMa wrote: On Tue, 27 Dec 2005 08:35:22 +1300, "Peter in New Zealand" peterbalplugATxtraSPOTcoSPOTnz wrote: Every year I send out a Christmas newsletter to quite a lot of children (grownup now, and grand children. I have established this as a family tradition over several years, and they all seem to enjoy their Christmas letter from Grandad. I use Word to compose it, with text and pictures, and it's always been a heap of fun. Each sub heading has always been prepared with alternative red and green letters, and looks real great in that context. It's just for the kids, and only once a year, but something to do it automatically would be a great labour saver to say the least. Peter, Way back in 2005, I asked how write a macro to automatically change the colors of individual letters in some text. You asked for a copy of the macro. Well, hello there! Voice from the past and all that. I got a copy back then, and I have used it every Christmas since. I honestly cannot remember where it came from, but it works great, so if you were the kind helper back then you have my grateful thanks, along with all my kids and grandkids. Just before last Christmas I sent out my family and friends Christmas newsletter to a much wider audience than previously , and over 70 copies were printed and sent, all with Christmas coloured text courtesy of the macro. Thank you again. Hello, yourself. :-) I guess I must have sent you a copy and forgot. Maybe I "improved" it since and meant to send you a copy of the improved version. Does your copy allow you to weight the colors? In the version I just posted, if you set vaColors to 2 reds and 1 green (red, red, green), two letters will be set to red for every one set to green. There might be a few other tweaks. Anyway, good to hear back from you and very glad you like the macro. Cheers -- Running Word 2000 SP-3 on Windows 2000 |
#4
Posted to microsoft.public.word.newusers
|
|||
|
|||
Random letter colors?
Does your copy allow you to weight the colors? In the version I just
posted, if you set vaColors to 2 reds and 1 green (red, red, green), two letters will be set to red for every one set to green. There might be a few other tweaks. Anyway, good to hear back from you and very glad you like the macro. Cheers -- Running Word 2000 SP-3 on Windows 2000 Ummmm, actually it does the job I was looking for so well that I never thought to try tweaking it. Might be fun though. Also I forgot to tell you that I have upgraded my computer twice since having the macro, and it has slotted in to Word 2000 with nary a twitch every time. Have also passed on a copy to a friend running Office XP, and it works great for him. Thinks - - - you could be onto a good thing here if this keeps up. :-) -- Peter in New Zealand. (Pull the plug out to reply.) Collector of old cameras, tropical fish fancier, good coffee nutter, and compulsive computer fiddler. |
Reply |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Convert to Symbols | Microsoft Word Help | |||
School Reports | Mailmerge | |||
Letter Wizard templates will not work | Mailmerge | |||
Can't save merged form letter... | Mailmerge | |||
How do I access a created template from letter wizard | Microsoft Word Help |