Word Interop InsertParagraphAfter和下一行样式

时间:2016-10-07 18:10:30

标签: c# ms-word ms-office office-interop

我正在迭代现有MS Word文档的段落并在特定级别的标题之后插入文本段落,但是当我插入文本时,它们继承了下面段落中的一些样式和/或下一个元素获取弄乱。这是我的代码:

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");
    }
}

这很有效,除非我有以下

标题2

  • 列表项
  • 列表项
  • 列表项

我的最终结果如下:

标题2

新文字

  • 列出项目

  • 列表项
  • 列表项

注意额外的空白项目符号。这是我的问题。

1 个答案:

答案 0 :(得分:1)

这适用于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();

        }
    }
}

请注意,我已经更改了添加文本和重置调用的顺序,并在文本末尾添加了\ r \ n字符(换行符)(没有换行符也会破坏我的列表但是它删除了来自第一个列表项的项目符号,我无法使用您的代码重现您的行为:))

如果表格跟随文档中的标题2样式标题,则上述代码将无法正常工作。

为此,我构建了能够正确创建表格上方段落的代码。

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();

        }
    }
}