Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2007 - Font.Color gives negative values 1

Status
Not open for further replies.

Nelviticus

Programmer
Sep 9, 2003
1,819
GB
I'm trying to do some automation on a huge document. This involves building a list of certain heading types but instead of using styles properly the various authors have just applied colours to the heading manually.

This means that the only way of identifying the heading type of a paragraph is check its colour:
Code:
para.Range.Font.Color
However, paragraphs with the same RGB value - in this case 63/166/204 - sometimes show up with a 'long' value of 13411903 and sometimes with -587137025. The same goes for other colours, sometimes it's the long you'd expect and sometimes it's a bizarre negative number.

Any idea why this might be, or how I can convert these negative numbers into their actual colour values? When you check the colour of the paragraph in the font dialog box it's correct - two blue paras will both be RGB 63/166/204 yet the code will report one as having a color of 13411903 and the other of -587137025. It's nuts!

Thanks

Nelviticus
 


Hi,

The 'negative' numbers are meaningless. The color value is 3 concatenated hex values.

Check this out...
Code:
Sub test()
    Dim i As Integer
'select your heading
    For i = 0 To 2
'see results in Immediate Window
        Debug.Print Hex(Selection.Range.Font.Color), _
            Mid(Hex(Selection.Range.Font.Color), i * 2 + 1, 2), _
            Hex2Dec(Mid(Hex(Selection.Range.Font.Color), i * 2 + 1, 2))
    Next
End Sub
Function Hex2Dec(sHEX As String) As Long
    Dim i As Integer, b As String
    
    For i = 1 To Len(sHEX)
        b = Mid(sHEX, i, 1)
        Select Case b
            Case "A" To "F"
                Hex2Dec = Hex2Dec + (Asc(b) - 65 + 10) * 16 ^ (Len(sHEX) - i)
            Case "0" To "9"
                Hex2Dec = Hex2Dec + CInt(b) * 16 ^ (Len(sHEX) - i)
        End Select
    Next
End Function

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
The answer to both your questions is 'yes'. I do have an idea of why this might be, and how you can convert the values, but it isn't always easy. I have written, quite extensively on it. See my website for all the gory details.

Briefly, the negative values represent Theme colours in Word 2007-format documents, and they are not absolute colours at all. One way to deal with this, depending on what else you are doing, and what your documents may contain, would be to save the documents in Word 97-2003 format; this will force the RGB values to the actual colours in the Theme at the time.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 



Drats, that 2007 thing!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Wow, excellent article Tony, have a star. I'm sure I can use some of that code to do the colour comparison test I need.

Thanks

Nelviticus
 
Tony, I'm not sure whether this is an error in your code or just Word weirdness but in order for your QueryColour function to work I had to modify your QueryThemeColor function.

My paragraph is blue with an RGB long value of -587137025. QueryThemeColor correctly gets the ThemeColorIndex (wdThemeColorText1/msoThemeDark1), but the DarknessByte and LightnessByte are both Unchanged so it never assigns a TintAndShade value, which means it has the default unassigned double value of zero - i.e. black.

Should QueryThemeColor set a default TintAndShade of 1? That makes it work for me but might break it for other purposes.

Regards

Nelviticus
 
I'm not sure what you mean. -587137025 (0xDD00FFFF) is, as you say, Theme Dark1, with no Tinting or Shading applied. Normally, this would be the Windows text colour, black, but it can, in theory, be anything.

Where are you picking up that colour from? Can you identify the source of the (blue) colour? What does it show in the Reveal Formatting Pane?

It's certainly possible that I have made an error in my code but I won't be able to look at it - or reply here again - until after Christmas.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
We use a custom theme here and Dark 1 is set to RGB 63/166/204. When iterating through ActiveDocument.Paragraphs and testing the .Range.Font.Color property, some of the blue paragraphs have a .Color value of 13411903 (manually-applied blue) and some have -587137025 (theme-applied Dark 1).

Using the code from your site I used the QueryColour function to find out what actual colour the -587137025 represents. If I use the code unmodified it returns a value of zero - i.e. black.

If I modify QueryThemeColour and add the initialisation line:
Code:
QueryThemeColor.TintAndShade = 1
then QueryColour returns the correct value of 13411903 (a rather unpleasant blue).

The 'reveal formatting' pane (which I didn't know existed, so thanks again!) shows that 'Font color' is 'Text 1'.

I just thought you might like to know that at the moment your code as posted doesn't always return the right colour (unless I've mis-read its purpose). However modifying the code works for me so I'll use it that way - thanks for all your hard work.

Regards

Nelviticus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top