How to: Delete the Comments 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 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.
Note |
---|
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 Sub WDDeleteComments(ByVal docName As String)
' Given a document name, remove all comments.
Const wordmlNamespace As String = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
Dim wdDoc As WordprocessingDocument = WordprocessingDocument.Open(docName, true)
If (Not (wdDoc.MainDocumentPart.WordprocessingCommentsPart) Is Nothing) Then
wdDoc.MainDocumentPart.DeletePart(wdDoc.MainDocumentPart.WordprocessingCommentsPart)
' Manage namespaces to perform Xml XPath queries.
Dim nt As NameTable = New NameTable
Dim nsManager As XmlNamespaceManager = 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.
Dim xdoc As XmlDocument = New XmlDocument(nt)
xdoc.Load(wdDoc.MainDocumentPart.GetStream)
' Retrieve a list of nodes representing the comment start elements and delete them all.
Dim nodes As XmlNodeList = xdoc.SelectNodes("//w:commentRangeStart", nsManager)
For Each node As System.Xml.XmlNode In nodes
node.ParentNode.RemoveChild(node)
Next
' Retrieve a list of nodes representing the comment end elements and delete them all.
nodes = xdoc.SelectNodes("//w:commentRangeEnd", nsManager)
For Each node As System.Xml.XmlNode In nodes
node.ParentNode.RemoveChild(node)
Next
' Retrieve a list of nodes representing the comment reference elements and delete them all.
nodes = xdoc.SelectNodes("//w:r/w:commentReference", nsManager)
For Each node As System.Xml.XmlNode In nodes
node.ParentNode.RemoveChild(node)
Next
' Save the document XML back to its document part.
xdoc.Save(wdDoc.MainDocumentPart.GetStream(FileMode.Create))
End If
End Sub
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
First, pass in a parameter representing the path to and the name of the source Word 2007 document.
Then, open the document as a WordprocessingDocument object.
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.