Word 2010 VBA - Manipulating Numbered Lists
I have found the ListFormat.ListLevelNumber to be unreliable.
I have a document someone sent me with a bulleted list that has a nested (level 2) list under one of the items. The nested list contains 3 subitems. Only subitem 2 reports that it is ListLevelNumber 2. The others continue to report ListLevelNumber = 1.
On a side note, the subitems that report the wrong list level have ListFormat.ListString set to the character used in level 2 of the list, so you might be able to work around the issue by checking both.
ListFormat.ListLevelNumber
is what you're looking for. Here is some code that will output the list level and text of every ListParagraph
in the document:
Sub listLevels()
Dim currentList As Range
Dim i, numLists As Integer
numLists = ActiveDocument.ListParagraphs.Count
For i = 1 To numLists
Set currentList = ActiveDocument.ListParagraphs(i).Range
MsgBox currentList.ListFormat.ListLevelNumber & " " & currentList.Text
Next
End Sub
You can of course use the condition of ListLevelNumber = 1
to access only top level lists, ListLevelNumber = 2
for second level, etc.
Is there a way to access, in one object, a list item, along with all its sub-items?
I don't really think there's a great way to do this, unless you build it yourself using recursion or something (create an object with an array of children, and each child with it's own array of children, etc.). I don't have this coded up, but hopefully the code I posted will let you accomplish what you want to do- and it's much simpler.
Also, ListFormat
also has some other members that may be useful if you're doing a lot with lists- dig around in the Object Browser to learn more.