Total Productivity Solutions, Inc. Company Logo
TPS Home
.NET
IIS
Networking
Search Engine
SharePoint
Add URL

Microsoft Word Automation - Specifying 24-bit color value for WDColor

When you are using the Word object model to create a word document from a .NET application you will more then likely come across a point when you need to change the color of an object you will need to specify a WDColor for the object. To specify the color you can use the WDColor enumeration to select one of the predefined colors or you can provide a 24-bit color that you cast as a WDColor.
To pass a 24-bit color value to the object you will first need to calculate the color value. To calculate the color value you can use the following equation:
value = (blue * 65536) + (green * 256) + red
The value that you calculate with the equation above is the same value that would be returned by the VB RGB function that is not present in C#. To use this value in the Word object model you will need to cast the value as a WdColor. The C# code below creates a paragraph in a word document, sets the color and the text of the paragraph:
//oDoc represents the Word document object
Object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
Word.Paragraph oPara1 = oDoc.Content.Paragraphs.Add(ref oRng);
oPara1.Range.Font.Color = ((Word.WdColor)8731929);
oPara1.Range.Text = “Test”;
oPara1.Range.InsertParagraphAfter();
Home Contact Us Site Map