Partager via


How to: Retrieve Property Values 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 document 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 a property value from a document 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.

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.

Retrieving Property Values from an Open XML Package

The following code retrieves the number of characters in an Office Word 2007 document by examining the ExtendedFileProperties part in the Open XML package representing the document.

' How to retrieve the properties of a document part.
Public Sub GetPropertyFromDocument(ByVal document As String)
   Dim xmlProperties As XmlDocument = New XmlDocument
   Dim wordDoc As WordprocessingDocument = WordprocessingDocument.Open(document, false)
   Dim appPart As ExtendedFilePropertiesPart = wordDoc.ExtendedFilePropertiesPart
      xmlProperties.Load(appPart.GetStream)
   Dim chars As XmlNodeList = xmlProperties.GetElementsByTagName("Characters")
      MessageBox.Show(chars.Item(0).InnerText)
End Sub
// How to retrieve the properties of a document part.
public static void GetPropertyFromDocument(string document)
{
   XmlDocument xmlProperties = new XmlDocument();

   using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, false))
   {
      ExtendedFilePropertiesPart appPart = wordDoc.ExtendedFilePropertiesPart;

      xmlProperties.Load(appPart.GetStream());
   }
   XmlNodeList chars = xmlProperties.GetElementsByTagName("Characters");

   MessageBox.Show(chars.Item(0).InnerText);
}

To retrieve the number of characters in a Word 2007 document by examining the ExtendedFileProperties part

  1. First, pass in parameters representing the path to and the name of the source Word 2007 document as well as the image file.

  2. Then, open the document as a WordprocessingDocument object.

  3. Next, create a reference to the MainDocumentPart part and create an ImagePart part.

  4. Finally, you read the contents of the image file as a FileStream object and write it to the imagePart part.