简体   繁体   English

C#将VBA宏转换为具有自定义RGB颜色的C#

[英]C# converting a VBA macro to C# with custom RGB color

In converting a VBA macro to a plugin coded in C#, I have run into the following impasse. 在将VBA宏转换为用C#编码的插件时,我遇到了以下僵局。

The original VBA code is: 原始的VBA代码为:

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: 使用Office.Interop命名空间转换为C#:

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. 由于RGB不是方法,因此无法编译此代码。 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: 更新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);
}

The RGB function you're actually calling in your VBA code, is located in the VBA standard library, in the Information module - at least according to Rubberduck 2.0 's context-sensitive status bar (disclaimer: I wrote that feature): 您实际上在VBA代码中调用的RGB函数位于VBA标准库的“ Information模块中-至少根据Rubberduck 2.0的上下文相关状态栏(免责声明:我编写了该功能):

Rubberduck 2.0的上下文相关状态栏

That RGB function really does nothing more than intake 3 numbers and output a corresponding RGB hex value. RGB函数实际上只需要输入3个数字并输出相应的RGB十六进制值即可。

This question asks specifically how to convert from System.Drawing.Color to a WdColor value - and the accepted answer looks pretty much exactly like your "too complex" code. 这个问题专门询问如何从System.Drawing.Color转换为WdColor值-接受的答案看起来很像您的“太复杂”代码。 Another solution would be to import Microsoft.VisualBasic and use the same Information.RGB function... but I cringe whenever I see the Microsoft.VisualBasic imported anywhere in a .NET project - it reeks of something being done wrong. 另一个解决方案是导入Microsoft.VisualBasic ,并使用相同Information.RGB功能...但我畏缩,每当我看到Microsoft.VisualBasic在.NET项目中的任何地方进口-这恶臭的东西正在做错误的。

Instead, you could make a simple extension method: 相反,您可以制作一个简单的扩展方法:

using System.Drawing;
using Microsoft.Interop.Word;

static class ColorExtensions
{
    public static WdColor ToWdColor(this Color color)
    {
        return (WdColor)(color.R + 0x100 * color.G + 0x10000 * color.B);
    }
}

Which turns your code to this: 这将您的代码变为:

var color = Color.FromArgb(173, 216, 230).ToWdColor();

To set the color from RGB decimal : 要从RGB十进制设置颜色:

Selection.Shading.BackgroundPatternColor = (Word.WdColor)(173 + 216 * 256 + 230 * 65536);

And from RGB hexadecimal: 从RGB十六进制开始:

Selection.Shading.BackgroundPatternColor = (Word.WdColor)0x00E6D8AD;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM