Export (0) Print
Expand All
Expand Minimize
0 out of 3 rated this helpful - Rate this topic

How to: Delete the Comments in a Word 2007 Document by Using the Open XML API

Office 2007

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 comment part 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.

NoteNote:

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.

Delete the Comments in a Document

In the following code, you delete the comments part from the document and the references in the main document part to the comments part:

public static void WDDeleteComments(string docName)
{
   // Given a document name, remove all comments.

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

   using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(docName, true))
   {
      if (wdDoc.MainDocumentPart.WordprocessingCommentsPart != null)
      {
         wdDoc.MainDocumentPart.DeletePart(wdDoc.MainDocumentPart.WordprocessingCommentsPart);
         // 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 document part into an XmlDocument instance:
         XmlDocument xdoc = new XmlDocument(nt);
         xdoc.Load(wdDoc.MainDocumentPart.GetStream());

         // Retrieve a list of nodes representing the comment start elements and delete them all.
         XmlNodeList nodes = xdoc.SelectNodes("//w:commentRangeStart", nsManager);
         foreach (System.Xml.XmlNode node in nodes)
         {
            node.ParentNode.RemoveChild(node);
         }

         // Retrieve a list of nodes representing the comment end elements and delete them all.
         nodes = xdoc.SelectNodes("//w:commentRangeEnd", nsManager);
         foreach (System.Xml.XmlNode node in nodes)
         {
            node.ParentNode.RemoveChild(node);
         }

         // Retrieve a list of nodes representing the comment reference elements and delete them all.
         nodes = xdoc.SelectNodes("//w:r/w:commentReference", nsManager);
         foreach (System.Xml.XmlNode node in nodes)
         {
            node.ParentNode.RemoveChild(node);
         }

         // Save the document XML back to its document part.
         xdoc.Save(wdDoc.MainDocumentPart.GetStream(FileMode.Create));
      }
   }
}

To delete the comments part from the document and the references in the main document part to that 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 document part containing the comments is checked to see if it exists and then is deleted.

    The remainder of the code uses XPath statements to remove the elements in the main document part that reference the comments part.

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.