2
votes

I am editing a Word document with five separate lists of bulleted items. If I reference, in my VBA code:

ActiveDocument.Lists(1).Range

then it refers to the entire section of the document from the first item in the first list to the last item in the last list.

If for some i > 1, I reference:

ActiveDocument.Lists(i)

in my code, then I get an error.

How could I fix my document so that each of the five bulleted lists in my document has a different index from 1 to 5?

2

2 Answers

1
votes

I was able to replicate by:

  1. Adding a bulleted list
  2. In the middle of the list, turning off bullets on one of the items.
  3. Typing some text on the now non-bulleted line.

When I do so, the corresponding List entry encompasses the range including the non-bulleted line in the middle.

I think the most realistic answer to your question is to save your document as a plain text file, open that, and re-apply the bullets where you want them. I am not finding anything online that talks about splitting an existing List.

That said, as far as I can tell, a non-bulleted line within the range of a List has Range.ListFormat.ListTemplate equal to Nothing, unlike the bulleted lines. So one option would be to iterate over your List record and store separate ranges, breaking based on ListTemplate. For example, the following function selects the lines in Lists(1) up to the first non-bulleted line (quick hack; YMMV):

Public Sub SelectList1()
    Dim r As Range
    Set r = ActiveDocument.Lists(1).Range
    Dim p As Paragraph, foundit As Boolean
    foundit = False

    For Each p In r.Paragraphs
        If p.Range.ListFormat.ListTemplate Is Nothing Then
            foundit = True
            Exit For
        End If
    Next p

    If foundit Then
        r.End = p.Range.Start
    End If

    r.Select
End Sub

For example, with this list created as described above:

 * foo
 * bar
bat
 * baz

Lists(1).Range includes foo through baz, including the intervening bar. Running SelectList1 (above) selects the foo and bar lines, but leaves bat and baz unselected.

0
votes

According to the MSDN:

List Object: Represents a single list format that's been applied to specified paragraphs in a document.

So if you have more than one list with some non-bulleted paragraph(s) in the middle, the Range will start with the first item of the first list and end with the last item of the last list including all non-bulleted paragraph(s) in the middle.

To fix this issue, you need to separate the lists (right-click on the bullet and select Separate List).

If you want to do it in code without modifying the document, you will have to iterate though the paragraphs of the Range of each List and check if it has a ListFormat.ListTemplate which indicates that it is a list item, otherwise it is a non-bulleted paragraph. Similar to cxw's answer, but iterating through all lists in case you have more than one.

This function highlights all list items and ignores the non-bulleted paragraph(s) in the middle:

Sub HighlightLists()
    Dim li As Word.List
    Dim p As Paragraph

    For Each li In ActiveDocument.Lists
        For Each p In li.Range.Paragraphs
            If Not p.Range.ListFormat.ListTemplate Is Nothing Then
                p.Range.HighlightColorIndex = wdYellow
            End If
        Next p
    Next li
End Sub