Search code examples
c#ms-wordoffice-interop

Is it possible to change a document builtin styles using c# interop.word?


I understand that one could simply use a template; But I am looking at the possibility of changing say the font size of Heading 1 and color as well.

I have tried something like this

Style style = Globals.ThisAddIn.Application.ActiveDocument.Styles.Add("Heading 1");
style.Font.Name = "Verdana";
style.Font.Size = 36;

The above denotes an error that heading 1 is a preserved name.

I have also found one suggesting something like this:

ActiveDocument.Styles("Heading 1").AutomaticallyUpdate = True
ActiveDocument.Styles("Heading 1").Font.Name = "Verdana" 

However in the above; Styles is not a method to take parameters. I have looked into some members of Styles class and it seems to have properties to change the styles like AutomaticallyUpdate; but can't figure out how to work it out.

So it is possible to change a builtin style to a different font or so using c# interop.word?


Solution

  • Yes, you can ...

    In C#

    Style style = ActiveDocument.Styles["Heading 1"];
    style.Font.Name = "Segoe UI";
    style.Font.Size = 48;
    

    In VBA

    Dim stl As Style
    Set stl = ActiveDocument.Styles("Heading 1")
    stl.Font.Name = "Segoe UI"
    stl.Font.Size = 48
    

    Now if you type something in the Heading 1 style it will be in Segoe UI with the size of 48.