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)
{
object firstRow = paragraph.Next().Range.Tables[1].Rows[1];
firstRow = paragraph.Next().Range.Tables[1].Rows.Add(ref firstRow);
paragraph.Next().Range.Tables[1].Split(2);
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();
}
}
}