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)?
NOTE: I'm using C#
but a VBA
solution will also be fine.
Snapshot of WORD document:
Code:
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();
}
Snapshot of Output window in VS2019
Item a
Item b
Item c
Item d
Item e
Item k
Desired output:
Item a
Item b
Item c
A new paragraph in the list item c
Item d
Item e
Item k
UPDATE:
The paragraph in list item 3
was created in a usual way as follows:
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......
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.