How to: Create and Modify Custom Document Properties

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Application-level projects

  • Document-level projects

Microsoft Office version

  • Excel 2007 and Excel 2003

  • PowerPoint 2007 and PowerPoint 2003

  • Project 2007 and Project 2003

  • Word 2007 and Word 2003

For more information, see Features Available by Application and Project Type.

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.

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 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?.

Example

Sub TestProperties()
    Dim properties As Microsoft.Office.Core.DocumentProperties
    properties = CType(Me.CustomDocumentProperties, Office.DocumentProperties)

    If ReadDocumentProperty("Project Name") <> Nothing Then
        properties("Project Name").Delete()
    End If

    properties.Add("Project Name", False, _
        Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString, _
        "White Papers")
End Sub 

Private Function ReadDocumentProperty(ByVal propertyName As String) As String 
    Dim properties As Office.DocumentProperties
    properties = CType(Me.CustomDocumentProperties, Office.DocumentProperties)

    Dim prop As Office.DocumentProperty

    For Each prop In properties
        If prop.Name = propertyName Then 
            Return prop.Value.ToString()
        End If 
    Next 

    Return Nothing 
End Function
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

Tasks

How to: Read from and Write to Document Properties

Concepts

Programming Application-Level Add-Ins

Programming Document-Level Customizations