Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
How to: Retrieve application property values from a word processing document
Collapse the table of content
Expand the table of content

How to: Retrieve application property values from a word processing document (Open XML SDK)

Office 2013

This topic shows how to use the classes in the Open XML SDK 2.5 for Office to programmatically retrieve an application property from a Microsoft Word 2013 document, without loading the document into Word. It contains example code to illustrate this task.

Last modified: March 22, 2013

Applies to: Office 2013 | Open XML

To use the sample code in this topic, you must install the Open XML SDK 2.5. You must explicitly reference the following assemblies in your project:

  • WindowsBase

  • DocumentFormat.OpenXml (installed by the Open XML SDK)

You must also use the following using directives or Imports statements to compile the code in this topic.

using System;
using DocumentFormat.OpenXml.Packaging;

To retrieve application document properties, you can retrieve the ExtendedFilePropertiesPart property of a WordprocessingDocument object, and then retrieve the specific application property you need. To do this, you must first get a reference to the document, as shown in the following code.

const string FILENAME = "DocumentProperties.docx";

using (WordprocessingDocument document = 
    WordprocessingDocument.Open(FILENAME, false))
{
    // Code removed here…
}

Given the reference to the WordProcessingDocument object, you can retrieve a reference to the ExtendedFilePropertiesPart property of the document. This object provides its own properties, each of which exposes one of the application document properties.

var props = document.ExtendedFilePropertiesPart.Properties;

Once you have the reference to the properties of ExtendedFilePropertiesPart, you can then retrieve any of the application properties, using simple code such as that shown in the next example. Note that the code must confirm that the reference to each property isn't null before retrieving its Text property. Unlike core properties, document properties aren't available if you (or the application) haven't specifically given them a value.

if (props.Company != null)
    Console.WriteLine("Company = " + props.Company.Text);

if (props.Lines != null)
    Console.WriteLine("Lines = " + props.Lines.Text);

if (props.Manager != null)
    Console.WriteLine("Manager = " + props.Manager.Text);

The following is the complete code sample in C# and Visual Basic.

using System;
using DocumentFormat.OpenXml.Packaging;

namespace GetApplicationProperty
{
    class Program
    {
        private const string FILENAME = 
            @"C:\Users\Public\Documents\DocumentProperties.docx";

        static void Main(string[] args)
        {
            using (WordprocessingDocument document = 
                WordprocessingDocument.Open(FILENAME, false))
            {
                var props = document.ExtendedFilePropertiesPart.Properties;

                if (props.Company != null)
                    Console.WriteLine("Company = " + props.Company.Text);

                if (props.Lines != null)
                    Console.WriteLine("Lines = " + props.Lines.Text);

                if (props.Manager != null)
                    Console.WriteLine("Manager = " + props.Manager.Text);
            }
        }
    }
}

Contribute to this article

Want to edit or suggest changes to this content? You can edit and submit changes to this article using GitHub.

Show:
© 2015 Microsoft