View Single Post
 
Old 01-26-2022, 12:27 AM
Peterson Peterson is offline Windows 10 Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 143
Peterson is on a distinguished road
Default

The following code loops through each paragraph in a doc; if it's H5 style, it's deleted, and any subsequent paragraphs that are either NOT a Heading style OR are H5 or higher are also deleted.
The code isn't working with track changes turned on or if there are redlines in the text and track changes is turned off, and it doesn't catch tables after an H5 heading. This is all I have for now.

Code:
Sub Heading5_Delete() ' 01/25/2022
    
    Dim myPara As Paragraph
    Dim blnHeading5Found As Boolean
    
    For Each myPara In ActiveDocument.Paragraphs
        ' If H5 has been found in a prior loop iteration,
        ' then check subsequent paragraphs:
        If blnHeading5Found = True Then
            ' If the Heading number is 5 or higher...
            If Mid(myPara.Range.Style, 9, 1) > 4 Then
                ' ...then delete the paragraph:
                myPara.Range.Delete
            ' Or if the style doesn't have "Heading" in it:
            ElseIf InStr(myPara.Style, "Heading") <> 1 Then
                ' ...then delete it:
                myPara.Range.Delete
            ' Otherwise, reset the "found" variable to false:
            Else
                blnHeading5Found = False
            End If
        End If
        
        ' If the paragraph style is Heading 5:
        If InStr(myPara.Range.Style, "Heading 5") = 1 Then
            ' ...then delete it:
            myPara.Range.Delete
            ' ...and set the "found" variable to true, in order to
            ' evaluate subsequent paragraphs:
            blnHeading5Found = True
        End If
    Next myPara
End Sub
Reply With Quote