Hi all,
I'm trying to think of a way to get a list of all text colors used in a document, without searching through a million possible colors. I'm stumped though. Any suggestions?
Thank you,
John
Hi all,
I'm trying to think of a way to get a list of all text colors used in a document, without searching through a million possible colors. I'm stumped though. Any suggestions?
Thank you,
John

This month, every Windows Secrets subscriber can download a one-chapter excerpt of Windows 7: The Missing Manual.Windows 7: The Missing Manual provides valuable information to help you overcome these difficulties in learning a new operating system. Subscribe today to download your free excerpt.
If I needed to do this then I would test each character in turn, and build an array of colour values. For each character I would search the array and add a member to the array if I couldn't find it already.
StuartR
Well, I did try this method. But it takes WAY too long to analyze every character individually.
Is there some collection that would tell me which colors are used in the document? If so, I can't find it.
I don't think there is a built-in list of "used colors"...
You might be able to do something using the HTML version of the word document. I don't have time to finish this as I have to go out in a minute, but you could start with
<code>
Sub CountColours()
Dim docTemp, docOriginal As Document
Set docOriginal = ActiveDocument
Set docTemp = Documents.Add
docTemp.Range.Text = docOriginal.HTMLProject.HTMLProjectItems(1).Text
<font color=448800> ' Search for 'Color: here and find the color details</font color=448800>
docTemp.Close SaveChanges:=False
Set docTemp = Nothing
End Sub
</code>
Then use docTemp.Content.Find to find every occurence of 'color: and parse out the next word.
StuartR
I got home again and found a bit of time to finish this. It creates an array containing one string for each colour used for text in the document. I am sure someone could make the code a bit more efficient, but this does work.
StuartR
<code>
Sub GetColours()
Dim docTemp, docOriginal As Document
Dim strColors(), strTemp As String
Dim i As Integer
Set docOriginal = ActiveDocument
Set docTemp = Documents.Add
docTemp.Range.Text = docOriginal.HTMLProject.HTMLProjectItems(1).Text
ReDim strColors(0)
With docTemp.Content.find
.Text = "'color:*'"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
Do While .Execute
strTemp = Replace(.Parent, "'", "") <font color=448800>' remove ' characters from the string</font color=448800>
strTemp = Replace(strTemp, "color:", "") <font color=448800>' this should just leave the color</font color=448800>
For i = 0 To UBound(strColors)
If strColors(i) = strTemp Then
strTemp = ""
Exit For
End If
Next i
If strTemp <> "" Then
If strColors(0) <> "" Then ReDim Preserve strColors(UBound(strColors) + 1) <font color=448800>' add an extra member to the array</font color=448800>
strColors(UBound(strColors)) = strTemp
End If
Loop
End With
docTemp.Close SaveChanges:=False
Set docTemp = Nothing
<font color=448800>' At this stage array strColors() contains names of all the colours found.</font color=448800>
End Sub
</code>
Thank you Stuart
A good lesson in ingenuity.
Regards
Don
I was intrigued by this and gave it a try. It didn't function in Word 2007, but I had luck in Word 2003. I looked at the temporary HTML document. Some of the colors in HTML document are readable, red, blue, etc. Otherse are numbers which don't make sense.
I wrote a macro that looks at the document character by character. I stored the colors in a Scripting Dictionary, thereby avoiding duplication (I use the Exists method to test before adding a new item). In this case, all of the values are the numeric values (255 for red, for example) that Word uses internally.
Is there a way to convert the standard values to text? There are about 60 named colors; non-standard color won't have a descriptive name.
You'd have to look up the symbolic constants in the Object Browser (F2) and write a Select Case ... End Select structure:
lngColor = ... ' color of character
Select Case lngColor
Case wdColorAqua
strColor = "Aqua"
Case wdColorAutomatic
strColor = "Automatic"
...
Case Else
strColor = "Other color: " & lngColor
End Select
I fussed with the very good macro that StuartR posted and added a bit of code to deal with situations where the color was followed by something else (following the semicolon) on the same line of HTML. The original macro worked very well, but there's the disadvantage that many of the colors are hex values.
I worked on another macro that checks each character in the document and also converts the Word enum value to a text value (e.g. "dark teal") so the list of colors makes more sense. This macro is very slow. (The txt file is in a separate post.)
Example code that's character-based.
> Some of the colors in HTML document are readable, red, blue, etc. Otherse are numbers which don't make sense.
These numbers are just 6 hex digits. You have a range of 0 to FF for each of Red, Green and Blue. FF is 255 in hex.
StuartR