Export (0) Print
Expand All

How to: Read from and Write to 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.

You can store document properties along with a document in the Microsoft Office applications listed above. These applications provide a number of built-in properties, such as author, title, and subject. This topic shows how to set document properties in Microsoft Office Excel and Microsoft Office Word.

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

To work with built-in properties in Excel, use the following properties:

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 code example shows how to change the built-in Revision Number property in a document-level project.

To change the Revision Number property in Excel

  1. Assign the built-in document properties to a variable.

    Microsoft.Office.Core.DocumentProperties properties;
    
    properties = (Microsoft.Office.Core.DocumentProperties)
        Globals.ThisWorkbook.BuiltinDocumentProperties; 
    
    Microsoft.Office.Core.DocumentProperty prop;
    prop = properties["Revision Number"]; 
    
  2. Increment the Revision Number property by one.

    if (prop.Value == null)
    {
        prop.Value = 1;
    }
    else
    {
        int revision;
        if (int.TryParse((string)prop.Value, out revision))
        {
            prop.Value = revision + 1;
            MessageBox.Show("Revision Number = " + revision);
        }
        else
        {
            MessageBox.Show("Revision Number = invalid value");
        }
    }
    

To work with built-in properties in Word, use the following properties:

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 code example shows how to change the built-in Subject property in a document-level project.

To change the Subject property

  1. Assign the built-in document properties to a variable.

    Microsoft.Office.Core.DocumentProperties properties;
    
    properties = (Microsoft.Office.Core.DocumentProperties)
        Globals.ThisDocument.BuiltInDocumentProperties; 
    
  2. Change the Subject property to "Whitepaper".

    // Set the Subject property. 
    properties["Subject"].Value = "Whitepaper"; 
    

Although you are working with Word and Excel and their objects, Microsoft Office supplies the list of available built-in document properties. Attempting to access an undefined property raises an exception.

Community Additions

ADD
Show:
© 2015 Microsoft