C# 将 VBA 宏转换为具有自定义 RGB 颜色的 C#
C# converting a VBA macro to C# with custom RGB color
在将 VBA 宏转换为用 C# 编码的插件时,我 运行 陷入了以下僵局。
原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)
使用 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);
此代码无法编译,因为 RGB
不是方法。我试图弄清楚如何使用可用的方法来做到这一点,但到目前为止运气不好。
我将不胜感激任何对此的建议或任何解释转换的描述。
更新:
实际上,它看起来像以下作品:
Color mycolor = Color.FromArgb(173, 216, 230);
Selection.Shading.BackgroundPatternColor = (Word.WdColor)(mycolor.R + 0x100 * mycolor.G + 0x10000 * mycolor.B);
This question 使用相同的方法。但是看起来还是太复杂了...
更新二:
根据下面的建议,这似乎是最顺利的方法:
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);
}
您在 VBA 代码中实际调用的 RGB
函数位于 VBA 标准库中的 Information
模块中 - 至少根据Rubberduck 2.0 的上下文相关状态栏(免责声明:我写了那个功能):
那个RGB
函数实际上无非就是输入3个数字并输出相应的RGB十六进制值。
This question 具体询问如何从 System.Drawing.Color
转换为 WdColor
值 - 接受的答案看起来与您的 "too complex" 代码非常相似。另一种解决方案是导入 Microsoft.VisualBasic
并使用相同的 Information.RGB
函数...但是每当我看到在 .NET 项目中的任何地方导入 Microsoft.VisualBasic
时,我都会感到畏缩——它散发出正在做的事情的味道错了。
相反,您可以制作一个简单的扩展方法:
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);
}
}
将你的代码变成这样:
var color = Color.FromArgb(173, 216, 230).ToWdColor();
从 RGB 十进制设置颜色:
Selection.Shading.BackgroundPatternColor = (Word.WdColor)(173 + 216 * 256 + 230 * 65536);
从 RGB 十六进制:
Selection.Shading.BackgroundPatternColor = (Word.WdColor)0x00E6D8AD;
在将 VBA 宏转换为用 C# 编码的插件时,我 运行 陷入了以下僵局。
原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)
使用 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);
此代码无法编译,因为 RGB
不是方法。我试图弄清楚如何使用可用的方法来做到这一点,但到目前为止运气不好。
我将不胜感激任何对此的建议或任何解释转换的描述。
更新:
实际上,它看起来像以下作品:
Color mycolor = Color.FromArgb(173, 216, 230);
Selection.Shading.BackgroundPatternColor = (Word.WdColor)(mycolor.R + 0x100 * mycolor.G + 0x10000 * mycolor.B);
This question 使用相同的方法。但是看起来还是太复杂了...
更新二:
根据下面的建议,这似乎是最顺利的方法:
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);
}
您在 VBA 代码中实际调用的 RGB
函数位于 VBA 标准库中的 Information
模块中 - 至少根据Rubberduck 2.0 的上下文相关状态栏(免责声明:我写了那个功能):
那个RGB
函数实际上无非就是输入3个数字并输出相应的RGB十六进制值。
This question 具体询问如何从 System.Drawing.Color
转换为 WdColor
值 - 接受的答案看起来与您的 "too complex" 代码非常相似。另一种解决方案是导入 Microsoft.VisualBasic
并使用相同的 Information.RGB
函数...但是每当我看到在 .NET 项目中的任何地方导入 Microsoft.VisualBasic
时,我都会感到畏缩——它散发出正在做的事情的味道错了。
相反,您可以制作一个简单的扩展方法:
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);
}
}
将你的代码变成这样:
var color = Color.FromArgb(173, 216, 230).ToWdColor();
从 RGB 十进制设置颜色:
Selection.Shading.BackgroundPatternColor = (Word.WdColor)(173 + 216 * 256 + 230 * 65536);
从 RGB 十六进制:
Selection.Shading.BackgroundPatternColor = (Word.WdColor)0x00E6D8AD;