Search code examples
c#ms-wordstylesoffice-interop

Customize Microsoft Word built-in style with numbering


I have a word document which already defined costumed built-in style. Like image below.

enter image description here

I want to change the style of the predefined built-in style by running the C# code below.

        // 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);

The code successfully change the color of heading text, but the number before the text still remains green, not affected by the code. Like the following picture.

enter image description here

Please give me some hint to also apply the color change to the numbering of heading.Thank you.


Solution

  • yes you can do that with the 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]);