Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-27-2018, 09:56 AM
Peterson Peterson is offline Best way to test for multiple scenarios, but just one outcome Windows 10 Best way to test for multiple scenarios, but just one outcome Office 2016
Competent Performer
Best way to test for multiple scenarios, but just one outcome
 
Join Date: Jan 2017
Posts: 141
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
  #2  
Old 09-27-2018, 03:52 PM
macropod's Avatar
macropod macropod is offline Best way to test for multiple scenarios, but just one outcome Windows 7 64bit Best way to test for multiple scenarios, but just one outcome Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Looping through all paragraphs in a long document and testing all possible combinations is liable to be quite slow. What I'd be inclined to use is a Find/Replace that looks for the primary Styles of interest, then check what precedes them, thus:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim ArrFnd, ArrPre, ArrRep, i As Long
ArrFnd = Array("List: Bullet", "List: Numbered")
ArrPre = Array("Body Text", "Body Text")
ArrRep = Array("Body Text: Pre-list", "Body Text: Pre-list")
For i = 0 To UBound(ArrFnd)
  With ActiveDocument.Range
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = ""
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindStop
      .Format = True
      .Style = ArrFnd(i)
      .Execute
    End With
    Do While .Find.Found
      If .Information(wdWithInTable) = True Then
        .End = .Tables(1).Range.End
      Else
        With .Paragraphs.First.Previous.Range.Paragraphs.First
          If .Style = ArrPre(i) Then .Style = ArrRep(i)
        End With
      End If
      If .End = ActiveDocument.Range.End Then Exit Do
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
Next
Application.ScreenUpdating = True
End Sub
Note how the code uses three arrays: ArrFnd holds a list of the primary Styles of interest; ArrPre holds a list of the preceding Styles to test; ArrRep holds a list of the preceding Styles to apply. It's essential that all the arrays have the same number of elements and that their entries are in the required order.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 09-28-2018, 08:36 AM
Peterson Peterson is offline Best way to test for multiple scenarios, but just one outcome Windows 10 Best way to test for multiple scenarios, but just one outcome Office 2016
Competent Performer
Best way to test for multiple scenarios, but just one outcome
 
Join Date: Jan 2017
Posts: 141
Peterson is on a distinguished road
Default

That did it -- thanks again for all your help, Paul!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help on using scenarios in Excel tjthomps23 Excel 0 01-20-2017 10:09 AM
How can I generate Scenarios? tommade Excel Programming 0 02-18-2016 04:56 PM
Lookup multiple values and compare different scenarios to get a specific result mws Excel 5 05-24-2014 04:52 AM
Best way to test for multiple scenarios, but just one outcome Creating a multiple choice test McDoug Office 2 10-19-2012 10:02 AM
Best way to test for multiple scenarios, but just one outcome How to make a multiple-choice test? micahfaulkner75 Mail Merge 1 09-12-2012 06:08 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:40 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft