I'm trying to take a numbered list created in Outlook and manipulate it based on the top-level list items. Unfortunately, the only way I've found to manipulate the list is through the ListParagraph type, which breaks out all list items (including sub-items) equally, instead of having different access for each level in the list.
Is there a way to access, in one object, a list item, along with all its sub-items?
Thanks.
Here's what I'm currently using, which works fine for lists with only one level of items:
While i <= oMeetingWordDoc.Lists(1).ListParagraphs.Count
Set oRange = oMeetingWordDoc.Lists(1).ListParagraphs(i).Range
*Perform actions with oRange
i = i + 1
wend
By lists with 'one level' I mean something like this:
By lists with 'sub-items' I mean something like this:
List item 1
a) Item a
b) Item b
c) Item c
Item 2
a) Item a
b) Item b
Item 3
a) Item a
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With