C# を使用して MS Word のアドインを開発しています。RGB 形式の Range オブジェクト内のテキストの色を見つける必要があります。
- RGB値を与えると思われるRange.Font.Colorを使用してみました。しかし、私はそれから負の範囲外の値を取得しています。
- Range.Font.TextColorで NotImplemented 例外が発生します。
Visual Studio 2010 を使用しています。助けてください。
これは、フォントの色の html スタイル タグをドキュメントに配置できる小さなテスト メソッドです (太字と斜体でこれを行う必要があり、色を取得できるかどうかを確認したかっただけです)。必要なものを入手してください。Word 用の c# vsto です
private void TEST()
{
Range currentWord = Globals.ThisAddIn.Application.ActiveDocument.Words.First;
object collapseStartObj = WdCollapseDirection.wdCollapseStart;
object oText = "";
object oMiss = System.Reflection.Missing.Value;
object oFindStop = WdFindWrap.wdFindStop;
object oCountOne = 1;
object oWordUnit = WdUnits.wdWord;
int count = 0;
while (currentWord != null)
{
count++;
currentWord.Find.Font.Bold = currentWord.Font.Bold;
currentWord.Find.Font.Italic = currentWord.Font.Italic;
currentWord.Find.Font.ColorIndex = currentWord.Font.ColorIndex;
string text = currentWord.Font.ColorIndex.ToString();
string thatColor = Regex.Replace(text, @"\d", ""); //remove any digits
string simpleColor = Regex.Replace(thatColor, "wd", "");//remove the wd
//MessageBox.Show(simpleColor); //for testing
currentWord.Find.Forward = true;
currentWord.Find.Format = true;
currentWord.Collapse(ref collapseStartObj);
currentWord.Find.Execute(ref oText, ref oMiss, ref oMiss, ref oMiss, ref oMiss, ref oMiss, ref oMiss, ref oFindStop, ref oMiss, ref oMiss, ref oMiss, ref oMiss, ref oMiss, ref oMiss, ref oMiss);
if (simpleColor != "NoHighlight")
{
try
{
string theText = currentWord.Duplicate.Text;
string thatText = Regex.Replace(theText, "\r", "");//get rid of carriage return
currentWord.Find.Execute(FindText: thatText, Format: true, ReplaceWith: "<font style = \"color:" + simpleColor + "\">^&</font>", MatchWildcards: true, Replace: Word.WdReplace.wdReplaceOne);
}
catch { }
}
currentWord = currentWord.Next(ref oWordUnit, ref oCountOne);
}
}
Word 2007 の WdColor を .NET Color クラスに変換するための Don Rotman の拡張メソッドを使用して、Range.Font.Color を System.Drawing.Color に 変換します。
MSWord.WdColor color = app.Selection.Range.Font.Color;
Color myColor = color.ToColor(); //ToColor is the extension method described in link
これで、 Range.Font.Color が WdOrange のような実際の列挙値を返さず、代わりに -654245889 のような値を返す場合でも、すべての RGB データを含む System.Drawing.Color オブジェクトに変換されます。
私のために働きます。それはあなたのために働きますか?