![]() |
|
#1
|
||||
|
||||
|
Is there a good way to test whether a character is one that would be displayed on screen and printed?
I'm working on a little macro to change each character in a string of text to a random color according to some theme. For the Christmas theme, each letter would be changed to red or green. For Halloween, orange or black. The macro limits the number of consecutive characters that can be assigned the same color. The problem is that non-printing characters, like space and tab, defeat the limit. If I set the limit to 3, then the macro will not allow more than three consecutive characters to be assigned the same color. But it will happily allow three characters on either side of a space to all be red as long as the space is green. But since the space is non-printing, it looks like there are 6 red characters. |
|
#2
|
|||
|
|||
|
Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oChar As Range
For Each oChar In Selection.Range.Characters
Debug.Print Asc(oChar) 'For information. Remove in final code.
Select Case Asc(oChar)
Case 65 To 90, 97 To 122, 48 To 57 'AZaz0-9
oChar.Font.Color = wdColorRed
Case 9, 10, 11, 13, 32, 160
'Skip
Case Else
'????
End Select
Next oChar
End Sub
|
|
#3
|
||||
|
||||
|
How about this? It turns any character in CharSet red and anything else green. In practice, it would skip them rather than turn them green.
Code:
Sub TestColorMacro()
Const MyName As String = "TestColorMacro"
Dim msg As String
msg = "Test the set of printable colors. Press Enter to continue or Cancel to abort."
Dim temp
temp = InputBox(msg, MyName, "OK")
If temp = "" Then Exit Sub
'Define the set of printable characters
Dim CharSet As String
CharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" _
& "abcdefghijklmnopqrstuvwxyz" _
& "1234567890`~!@#$%^&*()-_=+[]{}\|;:,.<>/?" _
& "'""" & Chr(145) & Chr(146) & Chr(147) & Chr(148)
Dim obChar As Range
For Each obChar In Selection.Characters
' 'Display the ASCII code
' msg = obChar & "=" & Asc(obChar)
' MsgBox msg
'If the character is in the set, color it red.
If InStr(CharSet, obChar) > 0 Then
obChar.Font.Color = RGB(255, 0, 0)
'Otherwise, color it green
Else
obChar.Font.Color = RGB(0, 255, 0)
End If
Next obChar
End Sub
|
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Printable Area issues | zaineyma | Word | 3 | 12-12-2012 09:11 AM |
HELP/ADVICE NEEDED. Testing Excel Accessibility guidelines
|
cmc89 | Excel | 1 | 03-29-2012 03:41 PM |
| Printable Entry Form | eJames | Excel | 1 | 01-07-2010 09:50 AM |
| Making template that is not printable | Askaleto | Word | 0 | 10-14-2009 11:45 AM |
| Defining Printable Areas | OfficeUser00939 | Word | 5 | 06-29-2009 09:04 AM |