Storing add-in properties with workbook - how to? (VSTO 2007)

T

Thomas

Hello Developers,

Where/how do I store Excel VSTO add-in properties with workbook? Is it
possible?

I would rather store some of my add-in properties somewhere within the
workbook, rather than in registry or in a separate file since those
properties are workbook specific.

Thanks for any pointers.

Thomas
 
J

Jim Thomlinson

One place to keep properties is in named ranges. Global properties can go in
global range names while sheet specific properties can be store in local
named ranges. They are easy to read from and write to. I know of at least one
commercial application that uses this technique quite successfully.
 
C

Colbert Zhou [MSFT]

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.msoPropertyTypeString, _
"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.CustomDocumentProperties;

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

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

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

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/library/dhxe2d75(VS.80).aspx

Have a nice day!

Best regards,
Ji Zhou
Microsoft Managed Newsgroup Support Team
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top