Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-26-2022, 12:27 AM
Peterson Peterson is offline Select Outlinelevel Windows 10 Select Outlinelevel 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
  #2  
Old 01-26-2022, 01:32 AM
Bikram Bikram is offline Select Outlinelevel Windows 10 Select Outlinelevel Office 2007
Advanced Beginner
Select Outlinelevel
 
Join Date: Jul 2021
Location: Nepal
Posts: 94
Bikram is on a distinguished road
Default

Thanks a lot peterson for your help.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to clear the 'OutlineLevel' property alex100 Word VBA 2 06-27-2020 11:47 PM
Select Outlinelevel Select a row in a different tab cosmicyes Excel Programming 1 07-09-2018 02:47 AM
Select Outlinelevel How to select row aymanharake Excel Programming 3 02-18-2017 04:09 PM
Select Outlinelevel How to select different words at once FTL Word 5 03-08-2016 06:10 PM
Select Outlinelevel select all tables majed_m Word VBA 5 07-30-2015 03:29 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:24 AM.


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