Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
 How to: Read from and Write to Docu...
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Studio Tools for the Microsoft Office System
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:

    Visual Basic
    Dim properties As Microsoft.Office.Core.DocumentProperties
    
    properties = DirectCast(Globals.ThisWorkbook.BuiltinDocumentProperties, _
        Microsoft.Office.Core.DocumentProperties)
    
    Dim prop As Microsoft.Office.Core.DocumentProperty
    prop = properties.Item("Revision Number")
    C#
    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:

    Visual Basic
    If prop.Value Is Nothing Then
        prop.Value = 1
    Else
        Dim revision As Integer
        If Integer.TryParse(prop.Value.ToString(), revision) Then
            prop.Value = revision + 1
            MessageBox.Show("Revision Number = " & revision)
        Else
            MessageBox.Show("Revision Number = invalid value")
        End If
    End If
    C#
    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:

    Visual Basic
    Dim properties As Microsoft.Office.Core.DocumentProperties
    
    properties = DirectCast(Globals.ThisDocument.BuiltInDocumentProperties, _
        Microsoft.Office.Core.DocumentProperties)
    C#
    Microsoft.Office.Core.DocumentProperties properties;
    
    properties = (Microsoft.Office.Core.DocumentProperties)
        Globals.ThisDocument.BuiltInDocumentProperties; 
  2. Change the Subject property to "Whitepaper":

    Visual Basic
    ' Set the Subject property.
    properties.Item("Subject").Value = "Whitepaper"
    C#
    // 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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
COM exception associated with the C# code above      rrogahn   |   Edit   |   Show History

I have tried to use the code above (in conjunction with the Office 2007 PIA's) and receive the following error when the cast is made:

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Core.DocumentProperties'. This ope
ration failed because the QueryInterface call on the COM component for the interface with IID '{2DF8D04D-5BFA-101B-BDE5-00AA0
044DE52}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))
.

Thanks in advance,

-Rick

Tags What's this?: Add a tag
Flag as ContentBug
RE: COM exception associated with the C# code above      McLean Schofield - MSFT   |   Edit   |   Show History

The code examples in this topic were designed to be used in document-level projects for Word 2003 and Excel 2003 in VSTO 2005. These projects can only be compiled against the Word 2003 or Excel 2003 PIAs on the development computer; otherwise, you will encounter errors such as the one you describe. However, the built solutions can be opened with Word 2007 or Excel 2007 and run as expected.

New document-level projects for Word 2007 and Excel 2007 will be included in the next version of VSTO, code named "Orcas".

You could also revise the code examples to work with application-level add-ins for Word 2007 or Excel 2007 (if you have VSTO 2005 SE installed). For example, in an add-in, you would replace Globals.ThisWorkbook.BuiltinDocumentProperties with this.Application.ActiveWorkbook.BuiltinDocumentProperties.

I hope this helps. If I have misunderstood your question in some way, please clarify your situation, or feel free to post a question on the VSTO forums (http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=16&SiteID=1).

McLean Schofield

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker