Iterating over all sheets in a Spreadsheet I ran into this error message:
Unable to cast transparent proxy to type ‘Microsoft.Office.Interop.Excel.Worksheet’.
This spreadsheet has some chart tabs that obviously cant be cast to a sheet. So when I run this code I get the above error:
foreach (Microsoft.Office.Interop.Excel.Worksheet activeSheet in xlApp.Sheets)
{
}
I’ve looked around to find a solution and a few people have encountered it – in different scenario’s to me eg:
http://social.msdn.microsoft.com/forums/en-US/vsto/thread/ab2e917a-d3bf-4e6a-84dc-7f8a9440fe0a
I am not happy with the workaround I have so far. I feel that exceptions should be used exceptionally and this isn’t one of those places:
for (int i = 0; i < xlApp.Sheets.Count; i++)
{
try
{
Worksheet wk = (Worksheet)xlApp.Sheets[i];
}
catch (Exception)
{
//This isn't a sheet - avoid sheet operations
}
}
I’m wondering if anyone knows how to iterate through the Sheets (skipping non-sheets) without the need for a Try-Catch? Is there some method or property in the Excel object model that can tell you if a sheet is not really a sheet?
What about:
Of course it would work only if you get the exception only for the chart tabs and not for the other sheets.