In converting a VBA macro to a plugin coded in C#, I have run into the following impasse.
The original VBA code is:
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 14
Selection.Font.Bold = True
Selection.Font.BoldBi = True
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = RGB(173, 216, 230)
Converted to C# with the Office.Interop
namespace:
using Microsoft.Office;
using Microsoft.Office.Interop;
using Word = Microsoft.Office.Interop.Word;
Word.Document oWordDoc = new Word.Document();
var Selection = oWordDoc.ActiveWindow.Selection;
Selection.Font.Name = "Times New Roman";
Selection.Font.Size = 14;
Selection.Shading.Texture = Word.WdTextureIndex.wdTextureNone;
Selection.Shading.ForegroundPatternColor = Word.WdColor.wdColorAutomatic;
Selection.Shading.BackgroundPatternColor = Word.ColorFormat.RGB(173, 216, 230);
This code won't compile as the RGB
is not a Method. I am trying to figure out how to do this with using the available methods, but no luck so far.
I would appreciate any advice on this or any description that would explain the conversion.
Update:
Actually, it looks like the following works:
Color mycolor = Color.FromArgb(173, 216, 230);
Selection.Shading.BackgroundPatternColor = (Word.WdColor)(mycolor.R + 0x100 * mycolor.G + 0x10000 * mycolor.B);
This question uses the same approach. But it still looks too complex...
Update 2:
With the suggestions below this seems to be the smoothest approach:
Selection.Shading.BackgroundPatternColor = RGB(172,216,230);
private Word.WdColor RGB(int p1, int p2, int p3)
{
return (Word.WdColor)p1 + (0x100 * p2) + (0x10000 * p3);
}