![]() |
|
#1
|
|||
|
|||
|
Hello
I was just wondering how can I do to get, in a word document, every letter or word of a different color? I guess there isn't any option to do it and I'll need a macro. Could you post a simple macro to do it, please? What about getting a gradient? I guess that each letter can only have one single solid color? regards |
|
#2
|
||||
|
||||
|
Yes, it would require a macro. The following shows how to randomly colour each letter individually - just don't expect quick results. Note too that some colours will look quite similar to others (some may even be the same).
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
Randomize Timer
With ActiveDocument
For i = 1 To .Characters.Count
.Characters(i).Font.Color = Int(Rnd * 1048576)
Next
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
Thank you, macropod, it works.
I've tried it at my girlfriend's laptop, a very old one, and it takes 1 minute to change the colors of a single page. Now I understand what some people says that macros are very slow. Regards |
|
#4
|
||||
|
||||
|
Actually, macros can do things very quickly. It's just that this one has a lot of work to do. Consider how long it would take you to change every character to a different colour manually and you'll get an idea of just how fast the macro is working. Of course, with a newer, faster PC, the macro would take even less time to do the job.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#5
|
|||
|
|||
|
But for a computer changing 1000 characters of colour is not too much. If I could program it with C++ (and worked not on a word document) it would be much faster.
I remeber using excel to perform calculus on very long rows and it gets really slow compared to other solutions. Anyway, it's nice. regards |
|
#6
|
||||
|
||||
|
A c++ solution that automates a Word document to do the same job would actually be slower (because of the automation overheads) and it would probably use exactly the same process ... Of course, if you were not working on a Word document, that would be a different issue, but in this case you are working on a Word document.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#7
|
|||
|
|||
|
Paul,
I've found that using a For .... Each rather that a For i = 1 to some.count is considerably faster in situations like this. Also by adding a collection and key you can prevent duplicate colors: Code:
Sub MyRandCharColors16M_Augment()
Dim oCol As New Collection
Dim lngColor As Long
Dim oChr As Range
Dim sngR As Single, sngG As Single, snbB As Single
Dim sngRGBSum As Single, sngRGBF As Single
Application.ScreenUpdating = False
'Define the maximum RGB sum. A score around 500-600 seems to give the best results.
Const sngRGBMax As Single = 550
Randomize
'Loop thru characters in document.
For Each oChr In ActiveDocument.Range.Characters
Dup_Reentry:
sngR = Int(Rnd() * 256)
sngG = Int(Rnd() * 256)
snbB = Int(Rnd() * 256)
'If the sum > max, scale all three back proportionately
sngRGBSum = sngR + sngG + snbB
If sngRGBSum > sngRGBMax Then
sngRGBF = sngRGBMax / sngRGBSum
sngR = sngR * sngRGBF
sngG = sngG * sngRGBF
snbB = snbB * sngRGBF
End If
'Assign the character its color
lngColor = RGB(sngR, sngG, snbB)
On Error GoTo Err_Duplicate
oCol.Add CStr(lngColor), CStr(lngColor)
oChr.Font.Color = lngColor
Next oChr
Application.ScreenUpdating = True
Exit Sub
Err_Duplicate:
Resume Dup_Reentry
Beep
End Sub
|
|
#8
|
||||
|
||||
|
Hi Greg,
Good point about For Each. I should have though of that.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#9
|
|||
|
|||
|
I can't address the girlfriends computer, but here using the revised code above a single page of solid text (3588 characters) takes about 3 seconds.
|
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
first letter moves to far right
|
tira | Word | 2 | 01-18-2013 12:33 PM |
Formatting a letter
|
Ceri | Word | 1 | 05-03-2012 02:28 AM |
Business letter
|
MK_Huef | Word | 1 | 03-27-2012 03:27 AM |
| Auto Letter | Hannes | Word | 1 | 10-29-2009 06:27 AM |
| Letter Templates | happymouth | Word | 8 | 05-17-2009 02:43 AM |