View Single Post
 
Old 01-17-2022, 07:34 AM
Shelley Lou Shelley Lou is offline Windows 10 Office 2016
Competent Performer
 
Join Date: Dec 2020
Posts: 170
Shelley Lou is on a distinguished road
Default VBA Help Indent Paragraphs between Headings

I have been working on updating the code over the weekend and have now got the code to update the body levels to be directly under the previous clause number if there are 2 or more clauses above the non-numbered text.

I just need to fathom out how to remove the numbering if there is only one paragraph between two headings now which I'm sure I'll get there in the end, I will keep researching until I find the answer.

Massive thank you to this forum and Graham in particular for helping me get this far with the code, couldn't have done it without you.

Code:
Sub TestChangeParas()
Dim para As Paragraph, nextPara As Paragraph
Dim oRng As Range
Dim i As Integer
    If Selection.Type = wdSelectionIP Then
        MsgBox Prompt:="You have not selected any text!"
        Exit Sub
    End If
    Set oRng = Selection.Range
    With oRng
        For Each para In oRng.Paragraphs
            For i = 1 To 7
                If para.Style Like "Heading " & i & "*" Then
                    Set nextPara = para.Next
                    If Not nextPara Is Nothing Then
                        If nextPara.Style = "Body Text" Then
                            nextPara.Style = "Body" & i
                        End If
                    End If
                End If
            Next i
        Next para
    End With
    Set oRng = Selection.Range
    With oRng.Find
    .Font.Bold = False
     For Each para In oRng.Paragraphs
     If para.Range.Style = "Body2" Then
     If para.Previous.Style = "Heading 2" Then
     para.Range.Style = "Body1"
End If
End If
If para.Range.Style = "Body3" Then
If para.Previous.Style = "Heading 3" Then
para.Range.Style = "Body2"
End If
End If
If para.Range.Style = "Body4" Then
If para.Previous.Style = "Heading 4" Then
para.Range.Style = "Body3"
End If
End If
If para.Range.Style = "Body5" Then
If para.Previous.Style = "Heading 5" Then
para.Range.Style = "Body4"
End If
End If
If para.Range.Style = "Body6" Then
If para.Previous.Style = "Heading 6" Then
para.Range.Style = "Body5"
End If
End If
If para.Range.Style = "Body7" Then
If para.Previous.Style = "Heading 7" Then
para.Range.Style = "Body6"
End If
End If
Next
lbl_Exit:
    Set para = Nothing
    Set nextPara = Nothing
    Set oRng = Nothing
    Exit Sub
    End With
End Sub
Reply With Quote