C#在Microsoft.Office.Interop.Word.List的列表项中获取第二段 [英] C# to get second paragraph in a list item of a Microsoft.Office.Interop.Word.List

查看:325
本文介绍了C#在Microsoft.Office.Interop.Word.List的列表项中获取第二段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using C# I am trying to get all list items of a list in a Microsoft WORD document. The document has only one list as shown below. And the third item of the list contains a second paragraph.

问题:以下代码未获得列表第三项的第二段.我可能会缺少什么,我们如何在输出中获得第二段(如下所示)?

Question: Following code is not getting the second paragraph of the third item of the list. What I may be missing and how can we get the second paragraph in the output (shown below)?

注意:我使用的是C#,但也可以使用VBA解决方案.

NOTE: I'm using C# but a VBA solution will also be fine.

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中的输出"窗口的快照

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中的段落是按以下常规方式创建的:

The paragraph in list item 3 was created in a usual way as follows:

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

Create the first list item by clicking on numbered list button on the ribbon (shown in image below). Then type Item a and hit Enter. Second list item automatically gets created. There you type Item b and hit Enter. Third list item automatically gets created. And so on......

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

Now that all 6 items get created, you go back to list item 3 where after the line Item c you hit Enter. A new list item as list item 4 gets created (and the remaining list items get renumbered - and the list has 7 items now). While still on the newly created list item 4, you then click on the numbered list button on the ribbon. The newly created list item 4 gets removed and gets replaced with a blank line where you type A new paragraph in the list item c. The list has 6 items now with a paragraph in list item 3.

推荐答案

如果这确实是一个独立的段落-ANSI 13(而不是新行-ANSI 11),则它不能成为其成员就Word而言,该列表.它打断了名单.这就是为什么ListParagraphs中不包含它的原因.

If this is truly an independent paragraph - ANSI 13 (and not new lines - ANSI 11) then it cannot be a member of the list as far as Word is concerned. It's interrupting the list. That's why it's not included in the ListParagraphs.

可能的是创建一个标准的Range对象(不区分List和非List段落)并将其循环.例如:

What is possible is to create a standard Range object (doesn't differentiate between List and non-List paragraphs) and loop that. For example:

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

这篇关于C#在Microsoft.Office.Interop.Word.List的列表项中获取第二段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆