none
WORD VBA or C# to Convert WORD's nested list to an HTML nested list RRS feed

  • Question

  • Using VBA (or c#) how can we read following multilevel nested list and create a string forming a nested HTML list as shown below. 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 a Word document::

    WORD's Nested List:

    String created from the above list (via VBA) that looks like the following 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>

    Tuesday, December 20, 2016 4:22 AM

Answers

  • Hi,

    We could use ListFormat.ListLevelNumber Property (Word) to identify which level it is.

    Sub Demo()
    Set a = ActiveDocument.Lists(1)
    For j = 1 To a.ListParagraphs.Count
    Set b = a.ListParagraphs(j)
    If b.Range.ListFormat.ListLevelNumber = 1 Then
    If b.Range.ListFormat.ListValue = 1 Then
    Debug.Print "<ol>"
    End If
    Debug.Print "<li>" & b.Range.Text & "</li>"
    If b.Range.ListFormat.ListValue = getMaxListVal(1) Then
    Debug.Print "</ol>"
    End If
    
    ElseIf b.Range.ListFormat.ListLevelNumber = 2 Then
    If b.Range.ListFormat.ListValue = 1 Then
    Debug.Print "<ul>"
    End If
    Debug.Print "<li>" & b.Range.Text & "</li>"
    If b.Range.ListFormat.ListValue = getMaxListVal(2) Then
    Debug.Print "</ul>"
    End If
    End If
    Next j
    End Sub
    
    Function getMaxListVal(listLvlNum As Integer)
    For a = 1 To ActiveDocument.Lists.Count
    For b = 1 To ActiveDocument.Lists(a).ListParagraphs.Count
    If ActiveDocument.Lists(a).ListParagraphs(b).Range.ListFormat.ListLevelNumber = listLvlNum Then
    temp = ActiveDocument.Lists(a).ListParagraphs(b).Range.ListFormat.ListValue
    End If
    Next b
    getMaxListVal = temp
    Next a
    End Function
    


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by namwam Wednesday, December 21, 2016 5:07 PM
    Wednesday, December 21, 2016 8:50 AM
    Moderator