This documentation is archived and is not being maintained.

How to: Replace the Header in a Word 2007 Document by Using the Open XML API

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 document parts stored in a single package. These packages combine the document parts that comprise 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 comprise the packages. This topic walks through the code and steps to replace the header 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 remove the header part in a document and replace it with a new document part containing custom XML from an external file.

public static void WDAddHeader(string docName, Stream headerContent)
   //  Given a document name, and a stream containing valid header content,
   //  add the stream content as a header in the document and remove the original headers.

   const string wordmlNamespace = "";
   const string relationshipNamespace = "";

   using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(docName, true))
      // Delete the existing header part.

      //  Create a new header part.
      HeaderPart headerPart = wdDoc.MainDocumentPart.AddNewPart<HeaderPart>();
      string rId = wdDoc.MainDocumentPart.GetIdOfPart(headerPart);                
      XmlDocument headerDoc = new XmlDocument();
      headerContent.Position = 0;

      //  Write the header out to its document part.

      //  Manage namespaces to perform XML 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 part into an XmlDocument instance.
      XmlDocument xdoc = new XmlDocument(nt);

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

         //  Create the new header reference node.
         XmlElement node = xdoc.CreateElement("w:headerReference", wordmlNamespace);
         XmlAttribute attr = node.Attributes.Append(xdoc.CreateAttribute("r:id", relationshipNamespace));
         attr.Value = rId;
         targetNode.InsertBefore(node, targetNode.FirstChild);

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

To remove the header part in a document and replace it with a new document part

  1. First, pass in parameters representing the path to and the name of the source Word 2007 document and the XML that defines the header that replaces the existing header in the document.

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

  3. Next, delete the existing header parts in the document and then create a blank header part.

  4. Then, create an XML document that is a temporary holder for the XML that describes the replacement header.

    The remaining code searches for the references to the header parts that you just deleted in the main document part by using XPath queries for the appropriate namespaces, deletes them, and inserts references to the new header part.

  5. Finally, the XML is saved back to the main document part.