I'm writing a VSTO add-in for Excel using C# and need to tie meta data to tables created in a worksheet which cannot be exposed to users or be copied when tables are duplicated in the worksheet. For that I'm using ListObject's Tag property which works well and seems to be intended for this use case. In order to set the Tag property I create the ListObject's VSTO Object as the property is not available in the interop object:
foreach (Microsoft.Office.Interop.Excel.Worksheet worksheet in Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets)
{
foreach (Microsoft.Office.Interop.Excel.ListObject table in worksheet.ListObjects)
{
Microsoft.Office.Tools.Excel.ListObject vstoTable = Globals.Factory.GetVstoObject(table);
vstoTable.Tag = new Tag { Identifier = 123 };
}
}
As soon as I create the VSTO object for the ListObject however, I cannot change the table's name any longer. I can still edit it in the table options UI but saving the workbook will automatically revert the name to whatever it was at the time the VSTO object was created. I tried disposing the VSTO object to no avail. The issue persists even if I do not set the Tag, just creating the VSTO object seems to be enough to trigger this. Interestingly enough, I am also creating the VSTO object counterparts for worksheets and set their Tag property but I can still rename those even after the objects have been created and tags have been set.
To verify that none of my other code impacts this I created a fresh add-in project with ONLY the above code snippet put on a ribbon button and the same issue occurs. I also tried using several other properties available in interop to avoid creating the VSTO object but they are all either exposed to users, get copied along with the table or both.
Clearly I am either using the objects incorrectly or this is an issue within Excel. Does anyone know how I can set the Tag without losing the ability to rename the table or if there is any other approach I could use to attach meta data to tables?