【问题标题】:Word Interop InsertParagraphAfter and next line styleWord 互操作 InsertParagraphAfter 和下一行样式
【发布时间】:2016-10-12 03:55:28
【问题描述】:

我正在遍历现有 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

新文本

  • 列表项

  • 列表项
  • 列表项

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

【问题讨论】:

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


    【解决方案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();
    
            }
        }
    }
    

    【讨论】:

    • 太棒了,这解决了这个特定问题。我仍然得到一个相关的问题。如果后面有一个表格而不是列表,它实际上会将文本插入表格的第一个单元格。真的很奇怪。对此有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    相关资源
    最近更新 更多