Export (0) Print
Expand All

How to: Create and Modify Custom Document Properties

NoteNote

Some code examples in this topic use the this or Me keyword or the Globals class in a way that is specific to document-level customizations, or they rely on features of document-level customizations such as host controls. These examples can be compiled only if you have the required applications installed. For more information, see Features Available by Product Combination.

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

Use the CustomDocumentProperties property to work with custom properties. This property returns 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 Excel and assign it a value.

Example

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", missing);
}

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;
}

Robust Programming

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

See Also

Community Additions

ADD
Show:
© 2014 Microsoft