api_name | api_type | ms.assetid | title | ms.suite | ms.author | author | ms.topic | ms.date | ms.localizationpriority | ||
---|---|---|---|---|---|---|---|---|---|---|---|
|
|
b0d3d890-431a-4838-89dc-1f0dccd5dcd0 |
How to: Get the contents of a document part from a package |
office |
o365devx |
o365devx |
conceptual |
11/01/2017 |
high |
This topic shows how to use the classes in the Open XML SDK for Office to retrieve the contents of a document part in a Wordprocessing document programmatically.
[!includeStructure]
The code starts with opening a package file by passing a file name to one of the overloaded Open() methods (Visual Basic .NET Shared method or C# static method) of the xref:DocumentFormat.OpenXml.Packaging.WordprocessingDocument class that takes a string and a Boolean value that specifies whether the file should be opened in read/write mode or not. In this case, the Boolean value is false specifying that the file should be opened in read-only mode to avoid accidental changes.
// Open a Wordprocessing document for editing.
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, false))
{
// Insert other code here.
}
' Open a Wordprocessing document for editing.
Using wordDoc As WordprocessingDocument = WordprocessingDocument.Open(document, False)
' Insert other code here.
End Using
[!includeUsing Statement]
[!includeStructure]
In this how-to, you are going to work with comments. Therefore, it is useful to familiarize yourself with the structure of the <comments> element. The following information from the [!includeISO/IEC 29500 URL] specification can be useful when working with this element.
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:
<w:comments>
<w:comment … >
…
</w:comment>
</w:comments>
The comments element contains the single comment specified by this document in this example.
© [!includeISO/IEC 29500 version]
The following XML schema fragment defines the contents of this element.
<complexType name="CT_Comments">
<sequence>
<element name="comment" type="CT_Comment" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>
After you have opened the source file for reading, you create a mainPart object by instantiating the MainDocumentPart. Then you can create a reference to the WordprocessingCommentsPart part of the document.
// To get the contents of a document part.
public static string GetCommentsFromDocument(string document)
{
string comments = null;
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
{
MainDocumentPart mainPart = wordDoc.MainDocumentPart;
WordprocessingCommentsPart WordprocessingCommentsPart = mainPart.WordprocessingCommentsPart;
' To get the contents of a document part.
Public Shared Function GetCommentsFromDocument(ByVal document As String) As String
Dim comments As String = Nothing
Using wordDoc As WordprocessingDocument = WordprocessingDocument.Open(document, True)
Dim mainPart As MainDocumentPart = wordDoc.MainDocumentPart
Dim WordprocessingCommentsPart As WordprocessingCommentsPart = mainPart.WordprocessingCommentsPart
You can then use a StreamReader object to read the contents of the WordprocessingCommentsPart part of the document and return its contents.
using (StreamReader streamReader = new StreamReader(WordprocessingCommentsPart.GetStream()))
{
comments = streamReader.ReadToEnd();
}
}
return comments;
Using streamReader As New StreamReader(WordprocessingCommentsPart.GetStream())
comments = streamReader.ReadToEnd()
End Using
Return comments
The following code retrieves the contents of a WordprocessingCommentsPart part contained in a WordProcessing document package. You can run the program by calling the GetCommentsFromDocument method as shown in the following example.
string document = @"C:\Users\Public\Documents\MyPkg5.docx";
GetCommentsFromDocument(document);
Dim document As String = "C:\Users\Public\Documents\MyPkg5.docx"
GetCommentsFromDocument(document)
Following is the complete code example in both C# and Visual Basic.