Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word 2010 VBA - Manipulating Numbered Lists

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:

  1. Item 1
  2. Item 2
  3. Item 3

By lists with 'sub-items' I mean something like this:

  1. List item 1

    a) Item a
    b) Item b
    c) Item c

  2. Item 2

    a) Item a
    b) Item b

  3. Item 3

    a) Item a

like image 786
Kevin Pope Avatar asked Dec 08 '11 00:12

Kevin Pope


2 Answers

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.

like image 133
jrichview Avatar answered Oct 04 '22 20:10

jrichview


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.

like image 40
Drew Gaynor Avatar answered Oct 04 '22 21:10

Drew Gaynor