Results 1 to 12 of 12
  1. #1
    New Lounger
    Join Date
    Mar 2008
    Location
    New York, New York, USA
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    List of all colors in doc (2007)

    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

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: List of all colors in doc (2007)

    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

  3. #3
    New Lounger
    Join Date
    Mar 2008
    Location
    New York, New York, USA
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: List of all colors in doc (2007)

    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.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: List of all colors in doc (2007)

    I don't think there is a built-in list of "used colors"...

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: List of all colors in doc (2007)

    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

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: List of all colors in doc (2007)

    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>

  7. #7
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: List of all colors in doc (2007)

    Thank you Stuart
    A good lesson in ingenuity.
    Regards
    Don

  8. #8
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: List of all colors in doc (2007)

    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.

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: List of all colors in doc (2007)

    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

  10. #10
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: List of all colors in doc (2007)

    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.)
    Attached Files Attached Files

  11. #11
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: List of all colors in doc (2007)

    Example code that's character-based.
    Attached Files Attached Files

  12. #12
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: List of all colors in doc (2007)

    > 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •