Thursday, May 15, 2008

C#: Convert System.Drawing.Color to Microsoft.Office.Interop.Word.WdColor

Using Microsoft.VisualBasic as an added reference to your C# projects, this is a smart way to convert Color to WdColor.

using VB = Microsoft.VisualBasic;
using Word = Microsoft.Office.Interop.Word;
...

Word.WdColor ConvertSystemColorToWdColor(System.Drawing.Color color)
{
int rgbColor = VB.Information.RGB(color.R, color.G, color.B);
Word.WdColor wdColor = (Word.WdColor)rgbColor;
return wdColor;
}