How to: Read from and Write to Document Properties
You can store document properties along with a document. Office 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.
For a related video demonstration, see How Do I: Access and Manipulate Custom Document Properties in Microsoft Word?.
Applies to: The information in this topic applies to document-level projects and VSTO add-in projects for the following applications: Excel; PowerPoint; Project; Word. For more information, see Features Available by Office Application and Project Type.
To work with built-in properties in Excel, use the following properties:
In a document-level project, use the BuiltinDocumentProperties property of the
ThisWorkbookclass.In a VSTO Add-in project, use the BuiltinDocumentProperties property of a Microsoft.Office.Interop.Excel.Workbook object.
These properties return a T:Microsoft.Office.Core.DocumentProperties object, which is a collection of T:Microsoft.Office.Core.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
Assign the built-in document properties to a variable.
Increment the
Revision Numberproperty by one.
To work with built-in properties in Word, use the following properties:
In a document-level project, use the BuiltInDocumentProperties property of the
ThisDocumentclass.In a VSTO Add-in project, use the P:Microsoft.Office.Interop.Word._Document.BuiltInDocumentProperties property of a Microsoft.Office.Interop.Word.Document object.
These properties return a T:Microsoft.Office.Core.DocumentProperties object, which is a collection of T:Microsoft.Office.Core.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
Assign the built-in document properties to a variable.
Change the
Subjectproperty to "Whitepaper".
The examples assume that you have written the code in the ThisWorkbook class in a document-level project for Excel, and the ThisDocument class in a document-level project for Word.
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.
Programming VSTO Add-Ins
Programming Document-Level Customizations
How to: Create and Modify Custom Document Properties