Kamran
Kamran

Reputation: 4100

How to select all the paragraphs between two bullets vba

I have some bullets in a MS Word Document type wdListSimpleNumbering, I am unable to find a way to select all the paragraphs between bullets u) and v).

E.g. In the document, I have following bullets;

u) Company Introduction.

1st paragraph

2nd paragraph

3rd paragraph

v) Company Vision

Now I am looking a way to select the paragraphs between the bullets u) and v).

With the following code; I can select the text with bullet u) but not sure how to select range up till bullet v). Here is my code:

For Each oPara In ActiveDocument.Paragraphs   
    If oPara.Range.ListFormat.ListType = wdListSimpleNumbering And _ 
       oPara.Range.ListFormat.ListString = "u)" Then _     
       oPara.Range.Font.ColorIndex = wdRed 
       oPara.Range.Select ' here I want to select paragraphs 
       Debug.Print (oPara)              
    End If
Next

Upvotes: 1

Views: 2387

Answers (1)

Christina
Christina

Reputation: 1379

Here's one way:

Sub TestSelectList()
Dim oPara As Paragraph
Dim bSelected As Boolean
For Each oPara In ActiveDocument.Paragraphs
    If oPara.Range.ListFormat.ListType = wdListSimpleNumbering And _
            oPara.Range.ListFormat.ListString = "u)" Then ' Make sure to remove underscore for a multiline THEN.
        oPara.Range.Font.ColorIndex = wdRed
        oPara.Range.Select 
        ' Set a flag indicating that you are currently within the desired list item.
        bSelected = True
        Debug.Print (oPara)
    ElseIf bSelected And oPara.Range.ListFormat.ListType <> wdListSimpleNumbering Then
        ' If the flag is positive and the current paragraph is not of the simple list type, include it in the selection.
        Selection.MoveEnd wdParagraph
    ElseIf oPara.Range.ListFormat.ListType = wdListSimpleNumbering Then
        ' Otherwise, if the paragraph is of the simple list type, turn off the flag.
        bSelected = False
    End If
Next
End Sub

This doesn't deal with situations where u) is the last item in the list, but I'm not sure what you'd want to do there, or how you'd know.

Upvotes: 1

Related Questions