Large Word Doc with many Duplicate Headings
Hey folks!
I have an issue I would love some help with. I have a document that gets spit out from an enterprise tool I use for work, and due to how things are set up, many Heading 2 headings are doubled.
I'm trying to get a macro together that will find Heading 2 headings and delete them if they have no content, OR if they are immediately followed by another Heading 2 heading that is identical (always the first Heading needs to be removed)
I tried this code:
Code:
Sub RemoveHeadingsWithNoContent()
Dim HeadingF As Range
Set HeadingF = ActiveDocument.Content
With HeadingF.Find
.Style = "Heading 2"
.Forward = True
.Wrap = wdFindStop
End With
Do
HeadingF.Find.Execute
If HeadingF.Find.Found Then
If HeadingF.Paragraphs(1).Next.Range.Style <> "Normal" Then
HeadingF.Delete
End If
End If
Loop While HeadingF.Find.Found
End Sub
That seems to work on smaller test documents, but the ones I need this macro to run on are from 4000 - 10000 pages long. When I run this on the proper document, it freezes up Word. I have tried leaving it overnight and it never recovers.
Any suggestions would be greatly appreciated. I am very new to VBA, so this is throwing me for a loop.