How to: Retrieve comments from a word processing document (Open XML SDK)
Published: December 04, 2012
This topic describes how to use the classes in the Open XML SDK 2.5 for Office to programmatically retrieve the comments from the main document part in a word processing document.
The following assembly directives are required to compile the code in this topic.
To open an existing document, instantiate the WordprocessingDocument class as shown in the following using statement. In the same statement, open the word processing file at the specified fileName by using the Open(String, Boolean) method. To open the file for editing the Boolean parameter is set to true. In this example you just need to read the file; therefore, you can open the file for read-only access by setting the Boolean parameter to false.
Using wordDoc As WordprocessingDocument = WordprocessingDocument.Open(fileName, False) ' Insert other code here. End Using
The using statement provides a recommended alternative to the typical .Open, .Save, .Close sequence. It ensures that the Dispose method (internal method used by the Open XML SDK to clean up resources) is automatically called when the closing brace is reached. The block that follows the using statement establishes a scope for the object that is created or named in the using statement, in this case wordDoc.
The comments and comment elements are crucial to working with comments in a word processing file. It is important in this code example to familiarize yourself with those elements.
The following information from the ISO/IEC 29500 specification introduces the comments element.comments (Comments Collection)This element specifies all of the comments defined in the current document. It is the root element of the comments part of a WordprocessingML document.Consider the following WordprocessingML fragment for the content of a comments part in a WordprocessingML document:
The following XML schema segment defines the contents of the comments element.
The following information from the ISO/IEC 29500 specification introduces the comment element.comment (Comment Content)This element specifies the content of a single comment stored in the comments part of a WordprocessingML document.If a comment is not referenced by document content via a matching id attribute on a valid use of the commentReference element, then it may be ignored when loading the document. If more than one comment shares the same value for the id attribute, then only one comment shall be loaded and the others may be ignored.Consider a document with text with an annotated comment as follows:This comment is represented by the following WordprocessingML fragment.
The following XML schema segment defines the contents of the comment element.
The following code example shows how to retrieve comments that have been inserted into a word processing document. To call the method GetCommentsFromDocument you can use the following call, which retrieves comments from a file named “Word16.docx,” as an example.
Dim fileName As String = "C:\Users\Public\Documents\Word16.docx" GetCommentsFromDocument(fileName)
The following is the complete sample code in both C# and Visual Basic.
Public Sub GetCommentsFromDocument(ByVal fileName As String) Using wordDoc As WordprocessingDocument = _ WordprocessingDocument.Open(fileName, False) Dim commentsPart As WordprocessingCommentsPart = _ wordDoc.MainDocumentPart.WordprocessingCommentsPart If commentsPart IsNot Nothing AndAlso _ commentsPart.Comments IsNot Nothing Then For Each comment As Comment In _ commentsPart.Comments.Elements(Of Comment)() Console.WriteLine(comment.InnerText) Next End If End Using End Sub