Unable to set line spacing in Word doc programatically with c# RRS feed

  • Question

  • I am new to c#. I'm trying to create a Word document programatically with list level. I have a rudimentary understanding of classes, methods, and namespaces. I can't get any formatting whatsoever in the code below to apply (the linespacing rules or tab indent). I'm trying to remove the 8 lines after each line in Word and set to 0.

    Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
                    para1.Range.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceExactly;
                    para1.Range.ParagraphFormat.SpaceAfter = 0.0f;
                    para1.Range.Paragraphs.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceExactly;
                    para1.Range.Paragraphs.SpaceAfter = 0.0f;
                    para1.Range.Text = "1. TESTING" + Environment.NewLine + "a. Line1" + Environment.NewLine + "b. Line2";

    • Moved by CoolDadTx Monday, July 3, 2017 2:04 PM Office related
    Sunday, July 2, 2017 5:43 PM

All replies

  • Hello,

    According to your description, you want to play with list level, but the code you are using is setting the paragraph format. And in the paragraph text, you add three line with Environment.NewLine, it actually add three paragraphs. 

    If you want to add list item, please test the code below:

                    Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
                    Word.Range range = Globals.ThisAddIn.Application.ActiveDocument.Range(0, 0);
                    Word.ListTemplate listTemplate = doc.Application.ListGalleries[Word.WdListGalleryType.wdOutlineNumberGallery].ListTemplates[1];
                    listTemplate.ListLevels[1].NumberFormat = "%1.";
                    listTemplate.ListLevels[1].NumberStyle =Word.WdListNumberStyle.wdListNumberStyleArabic;
                    listTemplate.ListLevels[2].NumberFormat = "%2.";
                    listTemplate.ListLevels[2].NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic;
                    range.ListFormat.ApplyListTemplateWithLevel(listTemplate, true, Word.WdListApplyTo.wdListApplyToWholeList, Word.WdDefaultListBehavior.wdWord10ListBehavior, 1);
                    int rangeLength = range.StoryLength - 1;
                    range.Text = "Test1";
                    Word.Range subRange = doc.Range(range.StoryLength - 1);
                    subRange.Text = "Test2";
                    Word.Range subRange2 = doc.Range(subRange.StoryLength - 1);
                    subRange2.Text = "Test3";
                    Word.Range range2 = doc.Range(range.StoryLength - 1);
                    range2.Text = "Test4";

    The result:

    I would suggest you share your expected result here. And you could record a macro when creating so that you could check what property and method you should use.



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Wednesday, July 5, 2017 9:08 AM