1
votes

Using MS Word VBA how can we convert the following nested list in a Word document to an html nested list. I know in Word-VBA ActiveDocument.Lists is a collection of all lists and ListParagraphsis a collection of list items in a list. But I'm unable to loop through these collections to get a handle of the following nested list in Word document:

Word document nested list:

enter image description here

HTML nested List:

<ol>
  <li>Test1</li>
  <li>Test2</li>
    <ul>
       <li>Test2a</li>
       <li>Test2b</li>
    </ul>
  <li>Test3</li>
  <li>Test4</li>
</ol>

UPDATE:

Using code from user @TimWilliams below I get the following result that actually treating sub-list as a separate list 2 (as shown below). But I need to know that the list 2 is indeed a nested list of list 1. How can I achieve this?

List: 1       Level: 1      Label: 1.     Text: Test1
List: 1       Level: 1      Label: 2.     Text: Test2
List: 1       Level: 1      Label: 3.     Text: Test3
List: 1       Level: 1      Label: 4.     Text: Test4
List: 2       Level: 2      Label: a)     Text: Test2a
List: 2       Level: 2      Label: b)     Text: Test2b
1
Maybe start with ListLevelNumber - stackoverflow.com/questions/8424573/…Tim Williams

1 Answers

2
votes

Should get you started:

Sub Tester()

    Dim doc As Document, l As List, lp, i, x

    Set doc = ActiveDocument

    For x = 1 To doc.Lists.Count

        Set l = doc.Lists(x)

        For i = 1 To l.ListParagraphs.Count
            Set lp = l.ListParagraphs(i).Range
            Debug.Print "List: " & x, _
                        "Level: " & lp.ListFormat.ListLevelNumber, _
                        "Label: " & lp.ListFormat.ListString, _
                        "Text: "; Replace(lp.Text, vbCr, "")

        Next i
    Next x

End Sub

EDIT: opening a fresh document and running the code above on your example list, this is the output I get.

List: 1       Level: 1      Label: 1.     Text: Test1
List: 1       Level: 1      Label: 2.     Text: Test2
List: 1       Level: 2      Label: a.     Text: Test2a
List: 1       Level: 2      Label: b.     Text: Test2b
List: 1       Level: 1      Label: 3.     Text: Test3
List: 1       Level: 1      Label: 4.     Text: Test4

Seems like our lists somehow got created differently: I used tab to indent my sub-list items and Shift-tab to get back to the top-level list.