View Single Post
 
Old 09-27-2018, 09:56 AM
Peterson Peterson is offline Windows 10 Office 2016
Competent Performer
 
Join Date: Jan 2017
Posts: 143
Peterson is on a distinguished road
Default Best way to test for multiple scenarios, but just one outcome

I'm stumbling through writing a macro that looks at each paragraph, determines if it's got a particular (list-type) style applied, then applies a related ("Body Text: Pre-list") style to the paragraph preceding any with a list-type style, provided that preceding paragraph is of a certain style ("Body Text").

I need to look for about six or seven different styles, plus some with manually applied bullets/numbers. What's the most efficient way to write an If statement that's got a ton of options, all of which, if met, will be addressed with the same subsequent action? I looked at Select Case, but that seems to be for when you're looking for multiple criteria but where each one has a different action taken. I'm pasting the code I've started on here. For now, it's only got two styles to look for.

Here's the crux of it: after I add all of the different options to the If statement, it's going to be really long. My Google tutor hasn't been terribly helpful...

Code:
Sub Format_BodyTextPreList3()
   
    Dim lngParaNum As Long
          
    ' The loop has to start with the *second* paragraph because if the first
    ' one is "List: Bullet" style, when the macro tries to shift up to the
    ' prior paragraph -- that is, paragraph 0 -- an error is thrown. This is
    ' all in case a user runs the macro on a document that just so happens to
    ' start with "List: Bullet" style.
    For lngParaNum = 2 To ActiveDocument.Paragraphs.Count
        
        If (ActiveDocument.Paragraphs(lngParaNum).Style = "List: Bullet" Or ActiveDocument.Paragraphs(lngParaNum).Style = "List: Numbered") Then
            Debug.Print ActiveDocument.Paragraphs(lngParaNum).Style
            lngParaNum = lngParaNum - 1
            If ActiveDocument.Paragraphs(lngParaNum).Style = "Body Text" Then
                Debug.Print ActiveDocument.Paragraphs(lngParaNum).Style
                ActiveDocument.Paragraphs(lngParaNum).Style = "Body Text: Pre-list"
            Else
                lngParaNum = lngParaNum + 2
            End If
        End If
    Next lngParaNum
        
   End Sub
Thanks
Reply With Quote