how to add text to the end of a list item, not to a new line

Viewed 32

Sample list below:

• List Item 1

• List item 2

End of list.

The VBA code is below:

For Each li In ActiveDocument.Lists
    For Each para In li.ListParagraphs
            para.Range.InsertAfter "aaaaaaa"
    Next para 
Next li

Then I run the code. I got the following result:

Sample list below:

• List Item 1

• aaaaaaaList item 2

aaaaaaaEnd of list.

What I expect is:

Sample list below:

• List Item 1aaaaaaa

• List item 2aaaaaaa

End of list.

1 Answers

There are two problems:

  • It looks like the For Each para In li.ListParagraphs starts with the second para. Therefore I am using the For i = - loop
  • The returned range ends as starting point of the next para - therefore we have to move the end before inserting the text
For Each li In ActiveDocument.Lists
    For i = 1 To li.ListParagraphs.Count
        Set rg = li.ListParagraphs(i).Range
        rg.Collapse wdCollapseEnd   'next para is selected :-/
        rg.MoveEnd WdUnits.wdCharacter, -1  'move one character back
        rg.Text = "aaa"
    Next
Next li

Related