首页 文章

Word Interop InsertParagraphAfter和下一行样式

提问于
浏览
0

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

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

这很有效,除非我有以下内容

Headers 2

  • 列出项目

  • 列出项目

  • 列出项目

我的最终结果如下:

Headers 2

新文本

  • 列出项目

  • 列出项目

  • 列出项目

注意额外的空白项目符号 . 那是我的问题 .

1 回答

  • 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字符(换行符)添加到文本的末尾(没有换行符也会破坏我的列表但是它正在删除子弹第一个列表项,我无法使用您的代码重现您的行为:))

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

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

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

相关问题