Reply
 
Thread Tools Display Modes
  #1   Report Post  
Frenchy
 
Posts: n/a
Default C# automation : coloring text with specific rgb or html color

Hi all,

I'm trying to color some text with colors that aren't defined in Word (ex
blue, dark-green,...)

To see how Word handles it I recorded a small macro, I selected a text and
colored It. Here's what It looks like;

Selection.Font.Color = 14897469

I'm trying and trying to create this number from a rgb or html color but I
can't find any function in Word or C# wich convert my colors in the Word colo
format

Any idea?
  #2   Report Post  
Jay Freedman
 
Posts: n/a
Default C# automation : coloring text with specific rgb or html color

Frenchy wrote:
Hi all,

I'm trying to color some text with colors that aren't defined in Word
(ex blue, dark-green,...)

To see how Word handles it I recorded a small macro, I selected a
text and colored It. Here's what It looks like;

Selection.Font.Color = 14897469

I'm trying and trying to create this number from a rgb or html color
but I can't find any function in Word or C# wich convert my colors in
the Word colo format

Any idea?


Hi Frenchy,

Given the R, G, and B values, the formula for the Color number is

((B * 256) + G) * 256 + R

Be sure to do this arithmetic with 16-bit or 32-bit variables (Long data
type in VBA) to prevent overflow.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org


  #3   Report Post  
Frenchy
 
Posts: n/a
Default C# automation : coloring text with specific rgb or html color

Thank you so much.

And do you know how I could transform a "html" color(ex:FF0000) to this
number?

If it isn't directly possible, I'm sure It's possible to transform it in rgb
then in "Word Color" format.

"Jay Freedman" wrote:

Frenchy wrote:
Hi all,

I'm trying to color some text with colors that aren't defined in Word
(ex blue, dark-green,...)

To see how Word handles it I recorded a small macro, I selected a
text and colored It. Here's what It looks like;

Selection.Font.Color = 14897469

I'm trying and trying to create this number from a rgb or html color
but I can't find any function in Word or C# wich convert my colors in
the Word colo format

Any idea?


Hi Frenchy,

Given the R, G, and B values, the formula for the Color number is

((B * 256) + G) * 256 + R

Be sure to do this arithmetic with 16-bit or 32-bit variables (Long data
type in VBA) to prevent overflow.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org



  #4   Report Post  
Jay Freedman
 
Posts: n/a
Default C# automation : coloring text with specific rgb or html color

Hi Frenchy,

See
http://msdn.microsoft.com/library/de...ors/colors.asp
for the whole nine yards.

The short version is that an HTML color number is really a string of three
2-digit hex numbers, which represent red, green, and blue in that order:
#RRGGBB.

Here's some example code to do the conversion:

Sub ColorDemo()
Dim HTMLcolor As String
Dim R As Long, G As Long, B As Long
Dim ColorNum As Long

' For example, "coral" is #FF7F50
HTMLcolor = "FF7F50"

' extract each pair of characters from the
' string, prefix it with &H, and convert it
' to a Long value
R = CLng("&H" & Mid$(HTMLcolor, 1, 2))
G = CLng("&H" & Mid$(HTMLcolor, 3, 2))
B = CLng("&H" & Mid$(HTMLcolor, 5, 2))

ColorNum = ((B * 256) + G) * 256 + R

Selection.Font.Color = ColorNum
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org

Frenchy wrote:
Thank you so much.

And do you know how I could transform a "html" color(ex:FF0000) to
this number?

If it isn't directly possible, I'm sure It's possible to transform it
in rgb then in "Word Color" format.

"Jay Freedman" wrote:

Frenchy wrote:
Hi all,

I'm trying to color some text with colors that aren't defined in
Word (ex blue, dark-green,...)

To see how Word handles it I recorded a small macro, I selected a
text and colored It. Here's what It looks like;

Selection.Font.Color = 14897469

I'm trying and trying to create this number from a rgb or html color
but I can't find any function in Word or C# wich convert my colors
in the Word colo format

Any idea?


Hi Frenchy,

Given the R, G, and B values, the formula for the Color number is

((B * 256) + G) * 256 + R

Be sure to do this arithmetic with 16-bit or 32-bit variables (Long
data type in VBA) to prevent overflow.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org



  #5   Report Post  
Frenchy
 
Posts: n/a
Default C# automation : coloring text with specific rgb or html color

Thanks, you're the pro in coloring

I'll try to transform it in c# tomorow, but It seems easy with your comments

"Jay Freedman" wrote:

Hi Frenchy,

See
http://msdn.microsoft.com/library/de...ors/colors.asp
for the whole nine yards.

The short version is that an HTML color number is really a string of three
2-digit hex numbers, which represent red, green, and blue in that order:
#RRGGBB.

Here's some example code to do the conversion:

Sub ColorDemo()
Dim HTMLcolor As String
Dim R As Long, G As Long, B As Long
Dim ColorNum As Long

' For example, "coral" is #FF7F50
HTMLcolor = "FF7F50"

' extract each pair of characters from the
' string, prefix it with &H, and convert it
' to a Long value
R = CLng("&H" & Mid$(HTMLcolor, 1, 2))
G = CLng("&H" & Mid$(HTMLcolor, 3, 2))
B = CLng("&H" & Mid$(HTMLcolor, 5, 2))

ColorNum = ((B * 256) + G) * 256 + R

Selection.Font.Color = ColorNum
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org

Frenchy wrote:
Thank you so much.

And do you know how I could transform a "html" color(ex:FF0000) to
this number?

If it isn't directly possible, I'm sure It's possible to transform it
in rgb then in "Word Color" format.

"Jay Freedman" wrote:

Frenchy wrote:
Hi all,

I'm trying to color some text with colors that aren't defined in
Word (ex blue, dark-green,...)

To see how Word handles it I recorded a small macro, I selected a
text and colored It. Here's what It looks like;

Selection.Font.Color = 14897469

I'm trying and trying to create this number from a rgb or html color
but I can't find any function in Word or C# wich convert my colors
in the Word colo format

Any idea?

Hi Frenchy,

Given the R, G, and B values, the formula for the Color number is

((B * 256) + G) * 256 + R

Be sure to do this arithmetic with 16-bit or 32-bit variables (Long
data type in VBA) to prevent overflow.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org




Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML text in Merge Field TM360 Mailmerge 3 August 20th 05 10:41 AM
converting text from html to normal mmakis Microsoft Word Help 1 August 11th 05 06:56 PM
Finding higlights with a specific color Peter New Users 4 August 5th 05 11:26 PM
Entire doc centers or bolds when selected specific text DURBAN_VIRGO Microsoft Word Help 1 January 13th 05 10:31 AM
I'm trying to change the text shadow color from gray to a darker . Chachilynch Microsoft Word Help 2 December 21st 04 12:54 AM


All times are GMT +1. The time now is 01:38 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"