0
votes

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.

2
Was it really necessary to have the title be the whole Question? You could have used, I dunno, Word Macro Help or something....MitMaro
Done. This is where Editing in SO comes handy. Otherwise, SO could have been easily littered with Garbage like other programming Q & A sites.Aamir
I agree, sadly can't edit yet. :PMitMaro

2 Answers

0
votes

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
0
votes

You don't need a macro: edit the document in Outline view. Navigate there and select only headings level 1, as follows:

View > Outline 

Next, select the level:

Show Level > Level 1.  

Delete only the H1 headings you no longer want to keep. You can drag & drop headings, and the subordinate content will follow.

This is a great view for doing large-scale editing as you require.