View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Colbert Zhou [MSFT] Colbert Zhou [MSFT] is offline
external usenet poster
 
Posts: 19
Default Storing add-in properties with workbook - how to? (VSTO 2007)

Hello Thomas,

We can store the add-in properties in Workbook.CustomDocumentProperties.

Microsoft Office Excel 2003 and Microsoft Office Word 2003 provide built-in
properties that are stored with workbooks and documents. In addition, you
can create and modify custom document properties if there is additional
information you want to store with the document.

Use the CustomDocumentProperties property to work with custom 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.

The following example demonstrates how to add a custom property in Excel
and assign it a value.
-----------------VB.NET-----------------------
Visual Basic Copy Code
Sub TestProperties()
Dim properties As Microsoft.Office.Core.DocumentProperties
properties = CType(Me.CustomDocumentProperties,
Office.DocumentProperties)

If ReadDocumentProperty("Project Name") < Nothing Then
properties("Project Name").Delete()
End If

properties.Add("Project Name", False, _
Microsoft.Office.Core.MsoDocProperties.msoProperty TypeString, _
"White Papers")
End Sub

Private Function ReadDocumentProperty(ByVal propertyName As String) As
String
Dim properties As Office.DocumentProperties
properties = CType(Me.CustomDocumentProperties,
Office.DocumentProperties)

Dim prop As Office.DocumentProperty

For Each prop In properties
If prop.Name = propertyName Then
Return prop.Value.ToString()
End If
Next

Return Nothing
End Function

-----------------------C#-------------------------------------------
C# Copy Code
void TestProperties()
{
Microsoft.Office.Core.DocumentProperties properties;
properties = (Office.DocumentProperties)this.CustomDocumentProp erties;

if (ReadDocumentProperty("Project Name") != null)
{
properties["Project Name"].Delete();
}

properties.Add("Project Name", false,
Microsoft.Office.Core.MsoDocProperties.msoProperty TypeString,
"White Papers", missing);
}

private string ReadDocumentProperty(string propertyName)
{
Office.DocumentProperties properties;
properties = (Office.DocumentProperties)this.CustomDocumentProp erties;

foreach (Office.DocumentProperty prop in properties)
{
if (prop.Name == propertyName)
{
return prop.Value.ToString();
}
}
return null;
}

You can get more information from the MSDN online documentations,
http://msdn.microsoft.com/en-us/libr...75(VS.80).aspx

Have a nice day!

Best regards,
Ji Zhou
Microsoft Managed Newsgroup Support Team