Export (0) Print
Expand All

How to: Create and Modify Custom Document Properties

The Microsoft Office applications listed above provide built-in properties that are stored with documents. In addition, you can create and modify custom document properties if there is additional information you want to store with the document.

Applies to: The information in this topic applies to document-level projects and application-level projects for the following applications: Excel 2013 and Excel 2010; PowerPoint 2013 and PowerPoint 2010; Project 2013 and Project 2010; Word 2013 and Word 2010. For more information, see Features Available by Office Application and Project Type.

Use the CustomDocumentProperties property of a document to work with custom properties. For example, in a document-level project for Microsoft Office Excel, use the CustomDocumentProperties property of the ThisWorkbook class. In an application-level project for Excel, use the CustomDocumentProperties property of a Microsoft.Office.Interop.Excel.Workbook object. These properties return a DocumentProperties object, which is a collection of DocumentProperty objects. You can use the Item property of the collection to retrieve a particular property, either by name or by index within the collection.

The following example demonstrates how to add a custom property in a document-level customization for Excel and assign it a value.

link to video For a related video demonstration, see How Do I: Access and Manipulate Custom Document Properties in Microsoft Word?.

void TestProperties()
{
    Microsoft.Office.Core.DocumentProperties properties;
    properties = (Office.DocumentProperties)this.CustomDocumentProperties;

    if (ReadDocumentProperty("Project Name") != null)
    {
        properties["Project Name"].Delete();
    }

    properties.Add("Project Name", false,
        Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString,
        "White Papers");
}

private string ReadDocumentProperty(string propertyName)
{
    Office.DocumentProperties properties;
    properties = (Office.DocumentProperties)this.CustomDocumentProperties;

    foreach (Office.DocumentProperty prop in properties)
    {
        if (prop.Name == propertyName)
        {
            return prop.Value.ToString();
        }
    }
    return null;
}

Attempting to access the Value property for undefined properties raises an exception.

Show:
© 2014 Microsoft