Вот небольшой тестовый метод, который может поместить теги стиля html для цвета шрифта в документ (мне нужно было сделать это для полужирного и курсивного начертания, и я просто хотел посмотреть, смогу ли я получить цвет). Если вы поиграете с ним, вы можете получите то, что вам нужно, это С# vsto для Word
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);
}
}
person
Chuck
schedule
11.07.2012