tags:

views:

182

answers:

1

I have a WORD document with a number of H1 headings. I'd like a macro that allows me to delete all the contents from a specific H1 heading until the next H1 heading - essentially deleting the H1 section. Similarly I might want to delete from a H2 heading until the next H1 or H2 heading.

A: 

You can determine the style of a paragraph using oParagraph.Style (where oParagraph is a Paragraph object). So, you could do something like:

Dim oStartHeadingParagraph As Paragraph
Set oStartHeadingParagraph = Selection.Paragraphs(1)

If oStartHeadingParagraph.Style <> "Heading 1" Then
    MsgBox "Please select the Heading 1 paragraph for the section you want to delete."
Else

    Dim oParagraph As Paragraph
    Set oParagraph = oStartHeadingParagraph

    Do While Not oStartHeadingParagraph.Next Is Nothing
        If oStartHeadingParagraph.Next.Style = "Heading 1" Then
            Exit Do
        Else
            oStartHeadingParagraph.Next.Range.Delete
        End If
    Loop

    oStartHeadingParagraph.Range.Delete

End If
Gary McGill