View Single Post
 
Old 06-08-2022, 06:25 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Try this way of doing it. I added an inputbox to deal with a situation where the first selected paragraph isn't a heading.
Code:
Sub ApplyNormNumStyles()
  Dim aPara As Paragraph, iLev As Integer, iStart As Integer, sStyName As String
  Dim bBold As Boolean, aRng As Range
  
  Set aRng = Selection.Range
  If Selection.Type = wdSelectionIP Then
    MsgBox Prompt:="You have not selected any text!"
    Exit Sub
  ElseIf aRng.Paragraphs(1).OutlineLevel = wdOutlineLevelBodyText Then
    iLev = InputBox("What level heading precedes your selected text?", "Starts at Level", "1")
  End If
  
  For Each aPara In aRng.Paragraphs
    sStyName = Split(aPara.Style, ",")(0)   'removes aliases from stylename
    aPara.Range.Select
    Select Case True
      Case sStyName Like "Heading *"           'keep track of the preceding heading level
        iLev = aPara.OutlineLevel
        bBold = aPara.Range.Words(1).Font.Bold
      Case sStyName Like "Body*", sStyName Like "Normal"
        If iLev < 8 And aPara.Range.Characters.Count > 1 Then
          If bBold Then
            aPara.Style = "Body" & iLev
          Else
            aPara.Style = "Body" & iLev - 1
          End If
        End If
    End Select
  Next aPara
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote