2
votes

I frequently generate Microsoft Word documents with headings that don't always have text underneath. I need a Macro which will check each heading and if there is no text underneath, delete the heading.

The headings have the styles "Heading 1", "Heading 2" and "Heading 3" and the following text will always have the style "Normal".

Would someone be able to suggest either a Macro to accomplish this or point me in the right direction to get started?

2

2 Answers

1
votes

For reference, editing Manu's code gave me this:

Sub test()

Dim HeadingF As Range
Set HeadingF = ActiveDocument.Content

With HeadingF.Find
    .Style = "Heading 1"
    .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
0
votes

Try this:

Sub test()

Dim HeadingF As Range
    Set HeadingF = ActiveDocument.Content

    With HeadingF.Find
        .Style = "Heading 1"
        .Forward = True
        .Wrap = wdFindStop
    End With

    Do
        HeadingF.Find.Execute
        If HeadingF.Find.Found Then

            If HeadingF.Paragraphs(1).Next.Range.Text = vbCr Then

                HeadingF.Delete

             End If
        End If
    Loop While HeadingF.Find.Found

End Sub