Word Macro: Deleting All Headers from an Active Document

This Word VBA macro deletes or removes all the headers from an active document.

Macro Example

Sub DeleteAllHeaders()

  Dim sec As Section
  Dim hdr As HeaderFooter

  For Each sec In ActiveDocument.Sections
    For Each hdr In sec.Headers
      hdr.Range.Delete
    Next
  Next sec

End Sub

Description

Variable Declaration

Sub...

  Dim sec As Section

The 3rd line declares an object variable named sec that refers to a Section object.
The Section object represents a single section in a document.

obj-Word-Section

Sub...

  Dim sec As Section
  Dim hdr As HeaderFooter

The 4th line declares an object variable named hdr that refers to a HeaderFooter object.
The HeaderFooter object represents a single header or footer.

obj-Word-HeaderFooter

Starts Looping Through All Sections

Sub...

  Dim sec As Section
  Dim hdr As HeaderFooter

  For Each sec In ActiveDocument.Sections

The 6th line starts to loop through all the elements (Section) of a collection (ActiveDocument.Sections).

(Application.)ActiveDocument

  For Each sec In ActiveDocument...

The Application.ActiveDocument property returns an active Document object.

obj-Word-Application-ActiveDocument

The ActiveDocument property is a member of the Word.Global class, so we can omit the Application property.

obj-Word-ActiveDocument

ActiveDocument.Sections(Document.Sections)

  For Each sec In ActiveDocument.Sections

The ActiveDocument.Sections (Document.Sections property) returns a Sections collection object that represents all the sections in a document.

obj-Word-Document-Sections

Starts Looping Through All Headers

Sub...

  Dim sec As Section
  Dim hdr As HeaderFooter

  For Each sec In ActiveDocument.Sections
    For Each hdr In sec.Headers

The 7th line starts to loop through all the elements (hdr = HeaderFooter) of a collection (sec.Headers = Section.Headers).

sec.Headers (Section.Headers)

The sec.Headers (Section.Headers property) returns a HeadersFooters collection (NOT Headers collection) that represents the headers for the section.

obj-Word-Section-Headers

Delete the Header

Sub...

  Dim sec As Section
  Dim hdr As HeaderFooter

  For Each sec In ActiveDocument.Sections
    For Each hdr In sec.Headers
      hdr.Range.Delete

The 8th line deletes the header.

hdr.Range (HeaderFooter.Range)

      hdr.Range...

The hdr.Range (HeaderFooter.Range property) returns a Range object that represents the portion of a document that is contained within the header.

obj-Word-HeaderFooter-Range

Range.Delete

      hdr.Range.Delete

And the Range.Delete method deletes the Range object.

obj-Word-Range-Delete

Loops Back

Sub...

  Dim sec As Section
  Dim hdr As HeaderFooter

  For Each sec In ActiveDocument.Sections
    For Each hdr In sec.Headers
      hdr.Range.Delete
    Next hdr
  Next sec

The 9th line loops back to get the next header.
The 10th line loops back to get the next section.

After every section is evaluated, this macro ends.

Sub DeleteAllHeaders()

  Dim sec As Section
  Dim hdr As HeaderFooter

  For Each sec In ActiveDocument.Sections
    For Each hdr In sec.Headers
      hdr.Range.Delete
    Next hdr
  Next sec

End Sub

Properties

http://www.relief.jp/itnote/archives/word-vba-delete-headers-footers.php

Apply To

  • Word 2013
  • Word 2010
  • Word 2007