JoshG
JoshG

Reputation: 145

VBA and MSWord: Use multiple values of a find parameter in Find/Execute routine

I have a Find/Execute routine that looks for paragraphs in my custom style, Bullet_Type_1_Level_1, which is a custom bulleted list style, and processes the paragraphs. (It checks each paragraph in the given range to see if it terminates in a period or not, but that's not important for this question). The routine currently works fine, but I want to expand it to search for additional levels--which translates into additional styles--of my outline list and to search for a style in another list, too. Is there a compact way to have my code also look for paragraphs in Bullet_Type_1_Level_2 and numlist_Level_1 (and process them, too) while it's at it? Here's the guts of my existing code:

For Each para In RangeToCheck.Paragraphs
With Selection.Find
    .Text = ""
    .Style = "Bullet_Type_1_Level_1"
    .Wrap = wdFindStop
    .Execute
    Do While .Found = True 'Look for the specified style
        strSentence = Selection.Text
        'Test the string using a block of code that I'm omitting, for brevity.
        'Finally, depending on what happened, put or don't a period at the end of the original range.        
End With
Next para

Upvotes: 1

Views: 44

Answers (2)

macropod
macropod

Reputation: 13505

An alternative approach that may be quicker if there are paragraphs that are none of those Styles:

Dim i As Long
For i = 1 To 3
  With RangeToCheck
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = ""
      .Replacement.Text = ""
      .Forward = True
      .Format = True
      .Wrap = wdFindStop
      .Style = "Bullet_Type_1_Level_" & i
      .Execute
    End With
    Do While .Find.Found = True
      If .InRange(RangeToCheck) = False Then Exit Do
      Select Case i
        Case 1 'Do something for Bullet_Type_1_Level_1
        Case 2 'Do something for Bullet_Type_1_Level_2
        Case 3 'Do something for Bullet_Type_1_Level_3
      End Select
      If ActiveDocument.Range.End = RangeToCheck.Range.End Then Exit Do
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
Next

Upvotes: 0

K.Dᴀᴠɪs
K.Dᴀᴠɪs

Reputation: 10139

You can add another loop.

Declare i (or more meaningful variable name), and loop through that.

Dim i As Long
For Each para In RangeToCheck.Paragraphs
    For i = 1 To 3
        With Selection.Find
            .Text = ""
            Select Case i
            Case 1
                .Style = "Bullet_Type_1_Level_1"
            Case 2
                .Style = "Bullet_Type_1_Level_2"
            Case 3
                .Style = "numlist_Level_1"
            End Select
            .Wrap = wdFindStop
            .Execute
            Do While .Found = True 'Look for the specified style
                strSentence = Selection.Text
                'Test the string using a block of code that I'm omitting, for brevity.
                'Finally, depending on what happened, put or don't a period at the end of the original range.
        End With
    Next i
Next para

Probably not the prettiest solution out there - word is not my strong point ☺.

Upvotes: 1

Related Questions