如何更改word文档样式中的字体颜色

时间:2016-09-10 01:25:15

标签: c# asp.net .net ms-word office-interop

我正在尝试使用office.interop更改word文档样式的字体颜色,但颜色没有变化。任何的想法? 我尝试了两种不同的方式。 第一种是尝试在ms字中更改标题样式的颜色: 这里有一些代码:

Application appWord=new Application();
Document doc=new Document();
ListGallery gallery=appWord.ListGalleries[WdListGalleryType.wdNumberGallery];
ListTemplate template =gallery.ListTemplates[4];
Style style=doc.ListTemplate[2];
style.LinkToListTemplate(template,1);
style.Font.ColorIndex=WdColorIndex.WdBlack;//doesn't work
doc.saveAs2(path);

第二种方法是在将文件插入ms doc后尝试设置范围或选择的颜色:

Paragraph p3 = wordDocument.Paragraphs.Add();
Range r3 = p3.Range;
//r3.Font.TextColor = WdColor.wdColorBlack;

var filename=String.Format("{0}Resources/TEST1.html", AppDomain.CurrentDomain.BaseDirectory);
String newString=System.IO.File.ReadAllText(filename).Replace("</body>","<p>1</p></body>");
System.IO.File.WriteAllText(filename, newString);
appWord.Selection.Font.ColorIndex = WdColorIndex.wdBrightGreen;
r3.InsertFile(filename);
//r3.Font.olorIndex = WdColorIndex.wdBrightGreen;

修改

这是解决方案:

(document.Styles[WdBuiltinStyle.wdStyleHeading2]).Font.ColorIndex = WdColorIndex.wdBlack;

感谢

2 个答案:

答案 0 :(得分:0)

你已经在程序中写了

style.Font.ColorIndex=WdColorIndex=WdBlack; //无效

将上述行更正为

style.Font.ColorIndex=WdColorIndex.WdBlack; // this will work

答案 1 :(得分:0)

编辑:

这是解决方案:

(document.Styles[WdBuiltinStyle.wdStyleHeading2]).Font.ColorIndex = WdColorIndex.wdBlack;

感谢