Note |
|---|
| 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
-
Assign the built-in document properties to a variable:
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")
Microsoft.Office.Core.DocumentProperties properties;
properties = (Microsoft.Office.Core.DocumentProperties)
Globals.ThisWorkbook.BuiltinDocumentProperties;
Microsoft.Office.Core.DocumentProperty prop;
prop = properties["Revision Number"];
-
Increment the Revision Number property by one:
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
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
-
Assign the built-in document properties to a variable:
Dim properties As Microsoft.Office.Core.DocumentProperties
properties = DirectCast(Globals.ThisDocument.BuiltInDocumentProperties, _
Microsoft.Office.Core.DocumentProperties)
Microsoft.Office.Core.DocumentProperties properties;
properties = (Microsoft.Office.Core.DocumentProperties)
Globals.ThisDocument.BuiltInDocumentProperties;
-
Change the Subject property to "Whitepaper":
' Set the Subject property.
properties.Item("Subject").Value = "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