weeMuckle
weeMuckle

Reputation: 33

Generating multilevel list in Word using interop C#

What I want:

  1. Heading 1

    1.1. Heading 2

       1.1.1. Heading 3
    

    1.2. Heading 2

  2. Heading 1

and so on.

I see that there exists a list style that accomplishes this, but I can't figure out how to code it.

Word.Range rng = wordDoc.Paragraphs.Add().Range;
rng.ListFormat.ApplyListTemplate(...);

I'm not sure how to fill the arguments of ApplyListTemplate(), or if that's even the correct approach. I can't find any actual examples of any ListTemplate object, only references to them.

Reference here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.listformat.applylisttemplate(v=office.15).aspx

Upvotes: 3

Views: 1629

Answers (1)

Chris
Chris

Reputation: 3519

I just finished doing this in a document-level add-in. It will be slightly different if yours is an application-level add-in.

Microsoft.Office.Interop.Word.Application app = Globals.ThisDocument.Application;
ListGallery gallery = app.ListGalleries[WdListGalleryType.wdOutlineNumberGallery];
// this one matches the numbering in your example, but not the indentation
ListTemplate myPreferredListTemplate = gallery.ListTemplates[5];

Style style = Globals.ThisDocument.Styles["Heading 1"];
style.LinkToListTemplate(myPreferredListTemplate, 1);

The galleries appear to be predefined app-level list styles, but you can also create and reuse doc-level ListTemplates through Document.ListTemplates.

Creating your own would be how you could get the indentation you need. You'd just have to play around with the settings in the ListTemplate.ListLevels.

Upvotes: 1

Related Questions