I'm working on an Excel Addon using VSTO. I've just run into a weird problem, if I try get a value from the CustomProperties it throws a COM exception and I have no idea why. Has anyone run into this? Here's the code I'm trying to use:
CustomProperties properties = worksheet.CustomProperties;
properties.Add("name", "value");
Console.Write(properties["name"]); //this crashes COMException. Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
Console.Write(properties[0]); //and this crashes COMException.Exception from HRESULT: 0x800A03EC
Console.Write(properties.Item[0]); //and this also crashes Exception from HRESULT: 0x800A03EC
Console.Write(properties.Item["name"]); //and this crashes as well Type mismatch.
Console.Write(properties.get_Item(0)); //this crashes again HRESULT: 0x800A03EC
Console.Write(properties.get_Item("name"); //and this still crashes Type mismatch.
I have no idea how to work with this collection. I found a workaround to read the properties:
Dictionary<string, string> workingProperties = new Dictionary<string, string>();
foreach (dynamic custProp in properties)
workingProperties.Add(custProp.Name, custProp.Value);
string value = workingProperties["name"];// this works
However, when it comes to updating an existing property - I'm stuck, I need a way to address an item in that collection, and there's no Remove or Delete function to first remove it and then add a new one with the same name.
Ok, was writing this post and realized a workaround:
string GetProperty(Worksheet ws, string name)
{
foreach (CustomProperty cp in ws.CustomProperties)
if (cp.Name == name)
return cp.Value;
return null;
}
void SetProperty(Worksheet ws, string name, string value)
{
bool found = false;
CustomProperties cps = ws.CustomProperties;
foreach (CustomProperty cp in cps)
{
if (cp.Name == name)
{
found = true;
cp.Value = value;
}
}
if (!found)
cps.Add(name, value);
}
Still, very weird that there's no way to access the collection in a normal way.
User contributions licensed under CC BY-SA 3.0