Export (0) Print
Expand All

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

With Microsoft Office Excel 2003 and Microsoft Office Word 2003, you can store document properties along with the document. Both Excel and Word provide a number of built-in properties, such as author, title, and subject. This example demonstrates how to change the Revision Number property in Excel and the Subject property in Word.

Setting Document Properties in Excel

Use the BuiltinDocumentProperties property of the ThisWorkbook class to work with built-in 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.

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

Setting Document Properties in Word

Use the BuiltInDocumentProperties property of the ThisDocument to work with built-in 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.

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

Robust Programming

The examples assume you have written the code in ThisWorkbook for Excel, and ThisDocument for Word.

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

See Also

Community Additions

ADD
Show:
© 2014 Microsoft