![]() |
|
#1
|
||||
|
||||
|
I found a list of built-in color index variables here: http://msdn.microsoft.com/en-us/libr...ffice.11).aspx I don't see one for orange. Is there one? If not, can I define one myself? |
|
#2
|
||||
|
||||
|
Hi Jennifer,
What you see is what you get and it's not extensible. You can, of course, work with the RGB equivalents.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
Jennfifer,
For more on RGB and orange see: http://simple.wikipedia.org/wiki/Orange HTML Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oShp As Word.Shape Set oShp = ActiveDocument.Shapes.AddShape(1, 50, 30, 50, 50) oShp.Fill.ForeColor.RGB = RGB(255, 160, 0) End Sub |
|
#4
|
||||
|
||||
|
Another approach, which returns the same colour as Greg's macro, is to define your own wdOrange value:
Dim wdOrange As Long wdOrange = 41215 Thus: Code:
Sub ScratchMacro() Dim oShp As Word.Shape, wdOrange As Long wdOrange = 41215 Set oShp = ActiveDocument.Shapes.AddShape(1, 50, 30, 50, 50) oShp.Fill.ForeColor.RGB = wdOrange End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#5
|
||||
|
||||
|
Paul,
Thanks for the suggestion, but I can't get it to work. Here's my little macro: Code:
Sub MyRandCharColors() Dim obChar As Range Dim ilColorNext As Word.WdColorIndex Dim wdOrange As Long wdOrange = 41215 Dim vaColors vaColors = Array(wdBlack, wdRed, wdBlue) 'vaColors = Array(wdBlack, wdOrange) Dim nsRnd As Single Randomize For Each obChar In Selection.Characters nsRnd = Rnd() ilColorNext = vaColors(Int((UBound(vaColors) + 1) * nsRnd)) obChar.Font.ColorIndex = ilColorNext Next obChar End Sub |
|
#6
|
|||
|
|||
|
Jennifer,
wdOrange is a Long variable not a wdColorIndex: Code:
Sub MyRandCharColors()
Dim oChr As Range
Dim sngRan As Single
Dim lngColorNext As Long
Dim wdOrange As Long
Dim varColors
wdOrange = 41215
varColors = Array(wdGreen, wdOrange, wdBlue)
Randomize
For Each oChr In Selection.Characters
sngRan = Rnd()
lngColorNext = varColors(Int((UBound(varColors) + 1) * sngRan))
On Error Resume Next
oChr.Font.ColorIndex = lngColorNext
If Err.Number <> 0 Then
oChr.Font.Color = lngColorNext
End If
Next oChr
End Sub
|
|
#7
|
||||
|
||||
|
Jennifer,
Defining wdOrange as a Long variable doesn't make it part of the wdColorIndex collection. As I said in my previous post: Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#8
|
||||
|
||||
|
Using the help from this thread, I added a new feature to my random color macro. The code snippet below will assign a random color from the entire color palette (16M colors) to each character in the selection. The result is kinda cool.
Code:
Sub MyRandCharColors() Dim oChr As Range Dim lngR As Long, lngG As Long, lngB As Long Randomize For Each oChr In Selection.Characters lngR = Rnd() * 256 lngG = Rnd() * 256 lngB = Rnd() * 256 oChr.Font.Color = RGB(lngR, lngG, lngB) Next oChr End Sub Code:
oChr.Font.Color = RGB(lngR, lngR, lngR) '256 shades of grey Should the Rnd() factor be 256 or 255? Rnd() returns a number on [0,1). That is, including 0, but not 1. If I use 255, I'll never get a 255 color value. Right? Should I convert lngR et al to integers (int(lngR))? It seems to work as is. Some of the colors this macro produces are too light. The next snippet makes an adjustment if the sum of the color values exceed some threshold. This seems to work pretty well. Code:
Sub MyRandCharColors()
Dim oChr As Range
Dim lngR As Long, lngG As Long, lngB As Long
Dim lngRGBSum As Long, lngRGBF As Long
Const lngRGBMax As Long = 500
Randomize
For Each oChr In Selection.Characters
lngR = Rnd() * 256
lngG = Rnd() * 256
lngB = Rnd() * 256
lngRGBSum = lngR + lngG + lngB
If lngRGBSum > lngRGBMax Then 'If sum > max, scale it down to max
lngRGBF = lngRGBMax / lngRGBSum
lngR = lngR * lngRGBF
lngG = lngG * lngRGBF
lngB = lngB * lngRGBF
End If
oChr.Font.Color = RGB(lngR, lngG, lngB)
Next oChr
End Sub
|
|
#9
|
|||
|
|||
|
Jennifer,
You will eventually get a 255 if you use 255. Try this: Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim i As Long
Dim lngRandom As Long
Do
i = i + 1
lngRandom = CLng(Rnd() * 255)
If lngRandom = 255 Then
MsgBox "Bingo! In " & i & " loops you reached max value."
Exit Do
End If
Loop
End Sub
|
|
#10
|
||||
|
||||
|
Your code gets 255 because it rounds the real number, rather than truncating it. Rnd returns a real number on [0,1). It can return 0, but it cannot return 1. Multiplying that by 255 converts it to a real number on [0,255). It can be zero, but it cannot be 255. However, it can be 254.999, which your statement rounds to 255.
The problem with this is that the 256 possible integers do not have the same probability. Try this code: Code:
Sub TestRnd() Dim lngCounter(6) As Long Dim i As Long Dim sngRnd As Single, lngRnd As Long For i = 1 To 10000 sngRnd = Rnd() * 5 'Get random real number on [0,5) lngRnd = sngRnd 'Round it [0,5] 0 & 5 get half weight lngRnd = Int(sngRnd) 'Truncate it [0,4] All numbers get full weight lngCounter(lngRnd) = lngCounter(lngRnd) + 1 Next i i = 0 End Sub With the code above, all 5 values get roughly equal tallies. As it turns out, the font.color statement Code:
oChr.Font.Color = RGB(sngR, sngG, sngB) Your post pointed out some other bugs in my code. I'll post a new version shortly. |
|
#11
|
||||
|
||||
|
OK. I think I have all of the bugs out. Here's the new code:
Code:
Sub MyRandCharColors16M()
Dim oChr As Range
'Use floating point so scaling will work.
'Font.Color will take floating point, but it rounds,
'which messes up the probabilities.
Dim sngR As Single, sngG As Single, sngb As Single
Dim sngRGBSum As Single, sngRGBF As Single
'Define the maximum RGB sum.
'A score around 500-600 seems to give the best results.
'See Random color macro test data.pdf.
Const sngRGBMax As Single = 550
Randomize
'Loop thru characters in selection one at a time.
For Each oChr In Selection.Characters
sngR = Int(Rnd() * 256) 'Select an integer on [0,255]
sngG = Int(Rnd() * 256) 'Int is required for equal probability
sngb = Int(Rnd() * 256) 'Else 0 only gets 0.5 and 255 gets 1.5
'If the sum > max, scale all three back proportionately
sngRGBSum = sngR + sngG + sngb
If sngRGBSum > sngRGBMax Then 'If sum > max, scale it down to max
sngRGBF = sngRGBMax / sngRGBSum
sngR = sngR * sngRGBF
sngG = sngG * sngRGBF
sngb = sngb * sngRGBF
End If
'Assign the next character its color
oChr.Font.Color = RGB(sngR, sngG, sngb)
Next oChr
End Sub
https://www.dropbox.com/sh/zm8we5ssjtf13fp/9vR8Pms7uO?m Someone on the sci.math newsgroup suggested that HSV color space would offer better color controls. Here's a link. I haven't had time to check it out. http://en.wikipedia.org/wiki/HSL_and_HSV If the same color value is used for all three RGB settings, we get shades of grey. Code:
Sub MyRandCharColors256G() Dim oChr As Range Dim sngGrey As Single Randomize For Each oChr In Selection.Characters ' sngGrey = Int(Rnd() * 256) '256 shades of grey sngGrey = Int(Rnd() * 192) '192 shades of grey ' sngGrey = Int(Rnd() * 128) '128 shades of grey oChr.Font.Color = RGB(sngGrey, sngGrey, sngGrey) Next oChr End Sub |
|
#12
|
|||
|
|||
|
Jennifer,
Thanks. Only comment is that Word 2003 seems to want to take its ball and go home after about 5000 characters. After that point no more characters are modified and UI events like (change font color) are frozen until the document is saved and reopened. I don't have time or interest in trying to figure out why or if anything can be done about it, just thowing it out for info. |
|
#13
|
||||
|
||||
|
Word 2007 does not appear to have that problem up to at least 100,000 characters. But there are some other side effects:
|
|
#14
|
|||
|
|||
|
Jennifer,
I'm sorry, I had checked with Word 2003, 2007, 2010 and 2013 and forgot that that issue was not apparent in the other versions. Unless MS does something in a Word2013 service pack then these type of process are going to be brutally slow (it current takes loops and for each process 3 to 4 time longer to run in Word 2013). I think the file size issue is because all the color attributes have to be written in the OpenOfficeXMLFile format file. Never notices the character count display issue before. |
|
#15
|
||||
|
||||
|
The slowness is not an issue for me. I only use this macro to create special effects on cards and envelopes for the grandkids. So it's usually only a title or a few paragraphs.
The macro has been fun to work on. I have a few more tweaks. If I like it, I'll post the complete macro. |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Run-time error '91': Object variable or With block variable not set
|
tinfanide | Excel Programming | 2 | 06-10-2012 10:17 AM |
variable Page references
|
scrinmemphis | Word | 4 | 06-06-2012 11:50 PM |
Capture a Font in a Variable
|
joatmon | Excel Programming | 1 | 05-30-2012 08:23 PM |
| Help with VBA macro - Variable input | sc30317 | Excel Programming | 0 | 08-31-2011 01:00 PM |
| Variable fields? | Emalee77 | PowerPoint | 0 | 01-30-2011 05:58 PM |