【发布时间】:2016-09-14 21:09:10
【问题描述】:
我正在尝试使用 office.interop 更改 word 文档样式的字体颜色,但颜色没有改变。任何想法? 我尝试了两种不同的方式。 第一个是尝试更改 ms word 中标题样式的颜色: 这是一些代码:
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;
谢谢
【问题讨论】:
-
@rajeeshmenoth 不太一样。我想更改样式的默认颜色,而不仅仅是选择。因为我将一个 html 文件插入到一个单词范围内!并且 html 文件包含标题样式!,
标签: c# asp.net .net ms-word office-interop