Export (0) Print
Expand All
Expand Minimize

How to: Delete the Headers and Footers in a Word 2007 Document by Using the Open XML API

Office 2007

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The Office Open XML Package specification defines a set of XML files that contain the content and define the relationships for all of the parts stored in a single package. These packages combine the parts that make up the document files for Microsoft® Office Excel® 2007, Microsoft Office PowerPoint® 2007, and Microsoft Office Word 2007. The Open XML object model allows you to create packages and manipulate the files that make up the packages. This topic walks through the code and steps to remove the header and footer parts in an Office Open XML package in Office Word 2007, although the steps are the same for each of the three 2007 Microsoft Office system programs that support the Office Open XML Format.


The code samples in this topic are in Microsoft Visual Basic® .NET and Microsoft Visual C#®. You can use them in an add-in created in Microsoft Visual Studio 2008. For more information about how to create an add-in in Visual Studio 2008, see Getting Started with the Open XML Format SDK 1.0.

In the following code, you delete the header part and footer part from the Office Open XML package and remove any references to those parts from the main document part:

// How to delete headers and footers from a document.
public static void WDRemoveHeadersFooters(string docName)
   // Given a document name, remove all headers and footers.

   const string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";

   using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(docName, true))
      if (wdDoc.MainDocumentPart.GetPartsCountOfType<HeaderPart>() > 0 ||
        wdDoc.MainDocumentPart.GetPartsCountOfType<FooterPart>() > 0)

         // Manage namespaces to perform XPath queries.
         NameTable nt = new NameTable();
         XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
         nsManager.AddNamespace("w", wordmlNamespace);

         // Get the document part from the package.
         // Load the XML in the document part into an XmlDocument instance.
         XmlDocument xdoc = new XmlDocument(nt);

         // Find the node containing the document layout.
         XmlNodeList layoutNodes = xdoc.SelectNodes("//w:sectPr", nsManager);
         foreach (System.Xml.XmlNode layoutNode in layoutNodes)
            // Delete any existing references to headers.
            XmlNodeList headerNodes = layoutNode.SelectNodes("./w:headerReference", nsManager);
            foreach (System.Xml.XmlNode headerNode in headerNodes)

            // Delete any existing references to footers.
            XmlNodeList footerNodes = layoutNode.SelectNodes("./w:footerReference", nsManager);
            foreach (System.Xml.XmlNode footerNode in footerNodes)

         // Save the document XML back to its document part.

To delete the headers and footers from the Open XML package and remove any references from the main document part

  1. First, pass in a parameter representing the path to and the name of the source Word 2007 document.

  2. Then, open the document as a WordprocessingDocument object.

  3. Next, the header and footer parts are checked to see if either exist and whether one or both are deleted.

    The remainder of the code uses XPath statements to remove the elements in the main document part that refer to the header part and footer part.

© 2014 Microsoft