View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Derrick[_3_] Derrick[_3_] is offline
external usenet poster
 
Posts: 8
Default Worksheet.CustomProperties

Hello all;

I'm messing around with Visual Studio Tools for Office 2003, particularly
Excel.

Was wondering if the Worksheet.CustomProperties collection has to be indexed
by an integer, or can you use the name of the property you are trying to
access? Since I don't think I can guarantee the order in which the
properties are added, I don't want to use integer indexing (though I could
loop through and check the property Name).

Specifically, this code snippet (which I fire from a CommandBar button)
always throws a Type Mismatch exception (hope you don't mind C#). Note that
the CustomProperty.Count is incrementing as expected; it blows up on the 2nd
line within the Try:

private void TestProperties(Excel.Worksheet activeSheet)
{
try
{
int i = activeSheet.CustomProperties.Count; //just checking if a
property was actually added

Excel.CustomProperty prop = activeSheet.CustomProperties["Test"];

if((bool)prop.Value == true)
{
MessageBox.Show("Test property was true");
}
else
{
MessageBox.Show("Test property was false");
}
}
catch(System.Runtime.InteropServices.COMException e) //property doesn't
exist, add it
{
activeSheet.CustomProperties.Add("Test", true);
//MessageBox.Show(e.Message);
}
}