Search code examples
c#ms-wordms-officeoffice-interop

Word Interop InsertParagraphAfter and next line style


I am iterating through paragraphs of an existing MS Word document and inserting paragraphs of text after Headings of a particular level, but when I insert the text, they inherit some of the styling from the paragraph below and/or the next element gets messed up. Here is my code:

foreach (Word.Paragraph paragraph in doc.Paragraphs)
{
    if (paragraph.get_Style(); != null && paragraph.get_Style() =="Heading 2")
    {
        paragraph.Range.InsertParagraphAfter();
        paragraph.Next().Reset();                            
        paragraph.Next().Range.Text = "New Text"
        paragraph.Next().set_Style("My Style");
    }
}

This works well, except if I have the following

Heading 2

  • List item
  • List item
  • List item

My end result looks like:

Heading 2

New Text

  • List item

  • List item
  • List item

Notice the extra blank bullet point. That is my problem.


Solution

  • this worked for me with Word 2013

    using Microsoft.Office.Interop.Word;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication7
    {
        class Program
        {
            static void Main(string[] args)
            {
                Application app = new Application();
                var doc = app.Documents.Open(@"C:\users\mhainc\desktop\test.docx");
    
                foreach (Microsoft.Office.Interop.Word.Paragraph paragraph in doc.Paragraphs)
                {
                    if (paragraph.get_Style() != null && paragraph.get_Style().NameLocal == "Heading 2")
                    {
                        paragraph.Range.InsertParagraphAfter();
                        paragraph.Next().Range.Text = "New Text\r\n";
                        paragraph.Next().Reset();
                        paragraph.Next().set_Style("Normal");
    
                    }
                }
                doc.Save();
                doc.Close();
    
            }
        }
    }
    

    Note that I have changed the order of adding the text and the reset call, and added \r\n characters (linefeed) to the end of the text (without the linefeed it destroyed my lists as well but it was removing the bullet from the first list item, I could not reproduce your behavior with your code :) )

    The aforementioned code will not work correctly if a table follows a Heading 2 styled heading in your document.

    For this I have constructed code that will correctly create the paragraph above your table.

    using Microsoft.Office.Interop.Word;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication7
    {
        class Program
        {
            static void Main(string[] args)
            {
                Application app = new Application();
                var doc = app.Documents.Open(@"C:\users\mhainc\desktop\test.docx");
    
                foreach (Microsoft.Office.Interop.Word.Paragraph paragraph in doc.Paragraphs)
                {
                    if (paragraph.get_Style() != null && paragraph.get_Style().NameLocal == "Heading 2")
                    {
                        bool afterTableSplit = false;
                        if (paragraph.Next().Range.Tables.Count > 0)
                        {
                            //add dummy row to the table
                            object firstRow = paragraph.Next().Range.Tables[1].Rows[1];
                            firstRow = paragraph.Next().Range.Tables[1].Rows.Add(ref firstRow);
                            //split the table after the dummy row
                            paragraph.Next().Range.Tables[1].Split(2);
                            //delete the dummy row table
                            paragraph.Next().Range.Tables[1].Delete();
                            afterTableSplit = true;
                        }
                        paragraph.Range.InsertParagraphAfter();
                        paragraph.Next().Range.Text = "New Text";
                        if (!afterTableSplit) paragraph.Next().Range.Text += "\r\n";
                        paragraph.Next().Reset();
                        paragraph.Next().set_Style("Normal");
    
                    }
                }
                doc.Save();
                doc.Close();
    
            }
        }
    }