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 |
#21
|
|||
|
|||
Random letter colors?
On Tue, 27 Dec 2005 20:23:26 -0800, LurfysMa
wrote: Excellent! Thanks. Here's my latest version. I have some questions at the end. You're really getting into this! Good stuff... I'll just throw in a couple of suggestions about your code before I get to the questions. '======================= 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 The sOption variable really should be declared as an Integer instead of a String. If you look at the VBA help topic on the MsgBox function, you'll see that it returns an Integer value to tell you which button was pressed, and the vbYes/vbNo/vbCancel constants are all integers. Your code functions because VBA does a silent conversion in the assignment and logical comparison operators, but it shouldn't have to. '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 It would be simpler here to write If sOption = vbCancel Then Call MsgBox("No action taken", , sTitle) Return If you test this by clicking the Cancel button, you'll get an error ("Return without GoSub"). Instead of Return, what you want here is Exit Sub. 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? There's no way in standard VBA to get a MsgBox to display any buttons except the ones listed in the help topic. I suspect there might be a way to use a Windows API call to modify the captions on the buttons, but I'd have to spend some time studying it. The alternative is to make a userform that looks like a message box, and contains whatever text and buttons you want. This is a little more complex, but if you think this kind of macro programming is fun, userforms will be a real blast. :-) Start at http://www.word.mvps.org/FAQs/Userfo...eAUserForm.htm, and let me know if you need help. 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 (!#$%...)? You can test against a list by replacing If oChar.Text = " " Or oChar.Text = vbCr Or oChar.Text = vbLf Then with If oChar.Text Like "[ ^13^10^9]" Then where the numeric codes are ^13 = vbCr, ^10 = vbLf, and ^9 = vbTab. The Like operator checks the oChar.Text to see if it's any of the characters inside the brackets. Alternatively, you can set up the loop this way: For Each oChar In Selection.Characters If oChar.Text Like "[A-Za-z0-9!#$%*]" Then If sOption = vbYes Then myColor = vaColors(Int((UBound(vaColors) + 1) * Rnd())) Else myColor = vaColors(iChar Mod (UBound(vaColors) + 1)) iChar = iChar + 1 End If Else myColor = vbBlack End If oChar.Font.ColorIndex = myColor Next oChar Now the coloring is done only for letters, digits, and the listed special characters, and anything else falls into the vbBlack case. 3. What's the best way to call the macro other than assigning it to a keyboard shortcut? You have these choices: - Keyboard shortcut - Toolbar button - Menu item - Entry or Exit macro of a protected form field - Intercept a built-in command such as Save or Print - Respond to an event such as opening, closing, switching to another window, etc. Which one is the "best" way depends on what the macro does and what your preference is. Often it's nice to have two or more ways for the same macro, for instance a shortcut and a menu item. Here are some references: http://www.word.mvps.org/FAQs/Custom...roToHotkey.htm http://www.word.mvps.org/FAQs/Custom...oToToolbar.htm http://www.word.mvps.org/FAQs/Macros...tSavePrint.htm http://www.word.mvps.org/FAQs/Macros...mentEvents.htm http://www.word.mvps.org/FAQs/Macros...tionEvents.htm Thanks. This has been kinda fun. Yeah, I know. It's been fun for me since Word 2.0. That's why I'm here. :-) -- 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. |
#22
|
|||
|
|||
Random letter colors?
On Wed, 28 Dec 2005 16:01:02 -0500, Jay Freedman
wrote: On Tue, 27 Dec 2005 20:23:26 -0800, LurfysMa wrote: Excellent! Thanks. Here's my latest version. I have some questions at the end. You're really getting into this! Good stuff... I'll just throw in a couple of suggestions about your code before I get to the questions. ....snip... '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 It would be simpler here to write If sOption = vbCancel Then I wasn't sure if Yes, No, and Cancel were the only possibilities. I just tried the Esc key and the little "X" icon and they both return a "2" just like Cancel. I just felt safer testing for the values I knew could be returned and allowing anything else (that I may not have known about) to cause an exit. So, unless there's so reason to change it, I think I'll leave it as is. 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 (!#$%...)? 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]]". Thanks for all the help. Now I am off to read up on user forms to get that MsgBox impersonator working. -- Running Word 2000 SP-3 on Windows 2000 |
#23
|
|||
|
|||
Random letter colors?
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. 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. -- 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. |
#24
|
|||
|
|||
Random letter colors?
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 |
#25
|
|||
|
|||
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 |
#26
|
|||
|
|||
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 |
#27
|
|||
|
|||
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 |
#28
|
|||
|
|||
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 |
#29
|
|||
|
|||
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? |
#30
|
|||
|
|||
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 |
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 |