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;
}

4 comments:

  1. im getting this error when i try the above exapmle: (Error 1 The type or namespace name 'Information' does not exist in the namespace 'Microsoft.VisualBasic' (are you missing an assembly reference?) \\fileprintsrv\mydocs\GSMITH\Visual Studio 2008\WebSites\HTMLConverter\App_Code\Class1.cs 474 26 \\fileprintsrv\mydocs\...\HTMLConverter\
    )


    Can you help me with problem.

    ReplyDelete
  2. Garth,
    Make sure you include the Microsoft.VisualBasic assemblies into your project's References first.
    Thanks
    KC

    ReplyDelete
  3. MS Word files are very popular in the computer world. This morning my friend has sent me some doc files on email. I opened theirs and didn't see anything,because of the files had been crashed. Fortunately I discovered on a soft blog - encoding for microsoft word. It helped me quite easy and without payment as I bore in mind.

    ReplyDelete

Please use your common sense before making a comment, and I truly appreciate your constructive criticisms.