Word Macro: Deleting All Headers and Footers from an Active Document

I’ve described a Word VBA macro that removes all the headers (not footers).
Today, I will explain a Word macro that removes all the headers and footers from an active document.

Macro Example

Sub DeleteAllHeadersFooters()

  Dim sec As Section
  Dim hd_ft As HeaderFooter

  For Each sec In ActiveDocument.Sections
    For Each hd_ft In sec.Headers
      hd_ft.Range.Delete
    Next
    For Each hd_ft In sec.Footers
      hd_ft.Range.Delete
    Next
  Next sec

End Sub

Description

This macro is, of cause, quite similar to the macro that remove all the headers:

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

The difference is a looping.

The previous deleting the only headers macro has one looping in the looping for all sections:

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

This deleting headers and footers macro has two looping in the looping for all sections:

  For Each sec In ActiveDocument.Sections
    For Each hd_ft In sec.Headers
      hd_ft.Range.Delete
    Next
    For Each hd_ft In sec.Footers
      hd_ft.Range.Delete
    Next
  Next sec

The 7th line starts looping through all the elements (header) of a collection (sec.Headers = Section.Headers):

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

And, the 10th line starts looping through all the elements (footer) of a collection (sec.Footers = Section.Footers):

  For Each sec In ActiveDocument.Sections
    For Each hd_ft In sec.Headers
      hd_ft.Range.Delete
    Next
    For Each hd_ft In sec.Footers

Properties

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

Apply To

  • Word 2013
  • Word 2010
  • Word 2007