Word 2010 VBA - Managing Numbered Lists

I am trying to take a numbered list created in Outlook and manipulate it based on top-level list items. Unfortunately, the only way I found to manipulate the list is with the ListParagraph type, which splits all the elements of the list (including sub-items) equally, instead of having different access for each level in the list.

Is there a way to access the list item in one object along with all its subitems?

Thanks.

Here is what I am currently using, which works fine for lists with only one level of elements:

While i <= oMeetingWordDoc.Lists(1).ListParagraphs.Count Set oRange = oMeetingWordDoc.Lists(1).ListParagraphs(i).Range *Perform actions with oRange i = i + 1 wend 

In "one level" lists, I mean something like this:

  • Paragraph 1
  • Point 2
  • Point 3

By lists with "subitems" I mean something like this:

  • List item 1

    a) Point a
    b) Point b
    c) Point c

  • Point 2

    a) Point a
    b) Point b

  • Point 3

    a) Element a

+7
source share
2 answers

ListFormat.ListLevelNumber is what you are looking for. Here is the code that displays the list level and the text of each 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 

Of course, you can use the ListLevelNumber = 1 condition to access only top-level lists, ListLevelNumber = 2 for the second level, etc.

Is there a way to access the list item in one object along with all its subitems?

Actually, I don’t think this is a great way to do this if you do not build it yourself using recursion or something else (create an object with an array of children, and each child will have its own array of children, etc. .). I do not have this encoding, but hopefully the code I posted will allow you to accomplish what you want to do, and it is much easier.

In addition, ListFormat also has some other members that can be useful if you do a lot with lists, which can be found in the Object Explorer to find out more.

+2
source

I found ListFormat.ListLevelNumber unreliable.

I have a document that someone sent me with a bulleted list that has a nested (level 2) list under one of the elements. The nested list contains 3 subitems. Only clause 2 reports that it is ListLevelNumber 2. The rest continue to report ListLevelNumber = 1.

On the side of the note, the subitems that report the wrong list have ListFormat.ListString set to the character used at level 2 of the list, so you can work around the problem by checking both.

+3
source

All Articles