首页 > 解决方案 > 使用编号自定义 Microsoft Word 内置样式

问题描述

我有一个 word 文档,它已经定义了装扮的内置样式。如下图。

在此处输入图像描述

我想通过运行下面的 C# 代码来更改预定义的内置样式的样式。

        // open document
        Object oFilePath = "C://Users/myDoc.docx";   
        Microsoft.Office.Interop.Word._Document myDoc;
        myDoc = wrdApp.Documents.Open(ref oFilePath, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing
                       );
        // Change header2 style
        myDoc.Styles[WdBuiltinStyle.wdStyleHeading2].Font.Color = WdColor.wdColorOrange;


        //save and close doc
        myDoc.Save();
        Object oFalse = false;
        myDoc.Close(ref oFalse, ref oMissing, ref oMissing);

代码成功更改了标题文本的颜色,但文本前面的数字仍然保持绿色,不受代码影响。就像下图一样。

在此处输入图像描述

请给我一些提示,以将颜色更改应用于标题编号。谢谢。

标签: c#ms-wordstylesoffice-interop

解决方案


是的,您可以使用 API 做到这一点

//To Cancel the numerotation
ListTemplate lt = null;
myDoc.Styles[WdBuiltinStyle.wdStyleHeading2].LinkToListTemplate(lt);

//To add First Level of Numerotation
ListGallery gallery = wrdApp.ListGalleries[WdListGalleryType.wdNumberGallery];
myDoc.Styles[WdBuiltinStyle.wdStyleHeading2].LinkToListTemplate(gallery.ListTemplates[1]);

推荐阅读