C# 获取 Microsoft.Office.Interop.Word.List 列表项中的第二段

C# to get second paragraph in a list item of a Microsoft.Office.Interop.Word.List

使用 C# 我试图获取 Microsoft WORD 文档中列表的所有列表项。该文档只有一个列表,如下所示。列表的第三项包含第二段。

问题: 以下代码没有获取列表第三项的第二段。我可能遗漏了什么以及我们如何获得输出中的第二段(如下所示)?

注意:我正在使用 C#,但 VBA 解决方案也可以。

WORD文档快照:

代码:

Using System
using Word = Microsoft.Office.Interop.Word;
....

static void Test()
{
    Word.Application oApp = new Word.Application();
    oApp.Visible = true;
    Word.Document oDoc = oApp.Documents.Open(@"C:\MyFolder\MyDoc.docx");
    string sList = "";

    Word.List oLst = oDoc.Lists[1];

    for (int j = 1; j <= oLst.ListParagraphs.Count; j++)
    {
        sList += oLst.ListParagraphs[j].Range.Text + "\n";
    }
    Console.Write(sList);
    sList = "";

    oDoc.Close(SaveChanges: Word.WdSaveOptions.wdDoNotSaveChanges);
    oApp.Quit();
}

VS2019 中输出 window 的快照

Item a
Item b
Item c
Item d
Item e
Item k

期望输出:

Item a
Item b
Item c
 A new paragraph in the list item c
Item d
Item e
Item k

更新:

list item 3中的段落是按以下常规方式创建的:

通过单击功能区上的 numbered list 按钮创建第一个列表项(如下图所示)。然后键入 Item ahit Enter。第二个列表项会自动创建。在那里输入 Item bhit Enter。第三个列表项会自动创建。等等……

现在所有 6 个项目都已创建,您返回 list item 3,在 Item c 行之后 hit Enter。一个新的列表项作为列表项 4 被创建(并且剩余的列表项被重新编号 - 并且列表现在有 7 个项目)。在新创建的列表项 4 上,然后单击功能区上的 numbered list 按钮。新创建的列表项 4 被删除并替换为您键入 A new paragraph in the list item c 的空行。该列表现在有 6 个项目,列表项目 3 中有一个段落。

如果这确实是一个独立的段落 - ANSI 13(而不是换行 - ANSI 11),那么就 Word 而言,它不能成为列表的成员。它打断了清单。这就是为什么它不包含在 ListParagraphs.

中的原因

可以创建一个标准的 Range 对象(不区分列表和非列表段落)并循环它。例如:

Word.List oLst = doc.Lists[1];
Word.Range startList = oLst.Range;
Word.Range endList = startList.Duplicate;
startList.End = endList.End;

for (int j = 1; j <= startList.Paragraphs.Count; j++)
{
    sList += startList.Paragraphs[j].Range.Text + "\n";
}