How to: Retrieve Comments from 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 parts that comprise the document files for Microsoft® Office Excel® 2007, Microsoft Office PowerPoint® 2007, and Microsoft Office Word 2007. The Open XML Application Programming Interface (API) allows you to create packages and manipulate the files that comprise the packages. This topic walks through the code to retrieve the comments from a Comments.xml part in an Office Open XML package in Office Word 2007, although the steps for retrieving information from a document part 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. |
Retrieving the Comments from an Open XML Package
In the following code, you retrieve the contents of the Comments.xml part in an Open XML package.
' How to get the contents of a document part.
Public Sub GetCommentsFromDocument(ByVal document As String)
Dim xmlComments As XmlDocument = New XmlDocument
Dim wordDoc As WordprocessingDocument = WordprocessingDocument.Open(document, false)
Dim mainPart As MainDocumentPart = wordDoc.MainDocumentPart
Dim WordprocessingCommentsPart As WordprocessingCommentsPart = mainPart.WordprocessingCommentsPart
xmlComments.Load(WordprocessingCommentsPart.GetStream)
MessageBox.Show(xmlComments.OuterXml)
End Sub
// How to get the contents of a document part.public static void GetCommentsFromDocument(string document)
{
XmlDocument xmlComments = new XmlDocument();
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, false))
{
MainDocumentPart mainPart = wordDoc.MainDocumentPart;
WordprocessingCommentsPart WordprocessingCommentsPart = mainPart.WordprocessingCommentsPart;
xmlComments.Load(WordprocessingCommentsPart.GetStream());
}
MessageBox.Show(xmlComments.OuterXml);
}
To retrieve the contents of the Comments.xml part
First, pass in parameters representing the path to and the name of the Word 2007 source document.
Next, declare an XmlDocument object.
Then, open the document as a WordprocessingDocument object.
Next, you create a reference to the WordprocessingCommentsPart part of the document and load the contents of the part into the in-memory copy of the XML document.
Finally, you display the contents of the WordprocessingCommentsPart part of the document in a dialog box.