If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#31
|
|||
|
|||
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 |
#32
|
|||
|
|||
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. |
#33
|
|||
|
|||
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 |
#34
|
|||
|
|||
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 |
#35
|
|||
|
|||
Random letter colors?
On Thu, 05 Jan 2006 00:46:13 -0800, LurfysMa
wrote: 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! ;-) Damn!!! That only appeared to work. Here's the fix. I didn't want the space included. The next character is the "!", so I modified the pattern to be: "[!-~" & Chr$(145) & "-" & Chr$(148) & "]" Unfortunately, the "!" is the character which causes the inverse set to be used. To get all characters between "!" and "~", use: "[""-~" & Chr$(145) & "-" & Chr$(148) & "!]" This works. (I think.) -- Running Word 2000 SP-3 on Windows 2000 |
#36
|
|||
|
|||
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 |
#37
|
|||
|
|||
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. |
#38
|
|||
|
|||
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 |
#39
|
|||
|
|||
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. |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
VBA "Rnd" Function: Truly Random? | TheRobsterUK | General Discussion | 2 | September 27th, 2005 04:50 AM |
word replaced or letter changed removes the next letter word?? | word replacement? | Running & Setting Up Queries | 1 | May 4th, 2005 03:39 AM |
Make a hidden phrase appear, letter by letter, in random order | Jimmy G | Powerpoint | 5 | April 29th, 2005 07:52 PM |
Random alpha generator | bj | Worksheet Functions | 3 | April 27th, 2005 11:02 PM |
New Letter to Contact | Gary | Contacts | 1 | July 23rd, 2004 02:36 PM |