Number List
Microsoft Word offers feature by which you can auto number your list in various formats. In this article we will write code to dynamically generate a numbered list in a document using C#.
C# Code example
using Word=Microsoft.Office.Interop.Word; public void CreateNumberList() { Word.Selection oSelection = Utility.WdApplication.Selection; //create list Word.ListTemplate oTemplate = Utility.WdApplication.ListGalleries[Word.WdListGalleryType.wdNumberGallery].ListTemplates[1]; Word.ListLevel oLevel= oTemplate.ListLevels[1]; oLevel.NumberFormat = "%1."; oLevel.TrailingCharacter= Word.WdTrailingCharacter.wdTrailingTab; oLevel.NumberStyle= Word.WdListNumberStyle.wdListNumberStyleArabic; oLevel.NumberPosition = Utility.WdApplication.InchesToPoints(0.25f); oLevel.TextPosition = Utility.WdApplication.InchesToPoints(0.5f); oLevel.TabPosition = 9999999; oLevel.ResetOnHigher = 0; oLevel.StartAt = 1; oSelection.Range.ListFormat.ApplyListTemplateWithLevel(oTemplate,false,Word.WdListApplyTo.wdListApplyToWholeList,Word.WdDefaultListBehavior.wdWord10ListBehavior); }
Note: You have to add Microsoft Word Interop library as reference to enable Using statement or create a Office solution (VSTO) which will have this library as default.
Next create a word document having list of paragraphs and make selection to the paragraphs which you want to convert into a formatted number list and run the code.
Output:
