Rectangle 27 0

c Why can't set cast an object from Excel interop?


Excel.Sheets
Excel.Workbook.Worksheets
Excel.Worksheets

@Zach & @Joel are right too, but you were the first to the starting gate, the the green check.

As long as you use the _Workbook.Worksheets property instead of the _Workbook.Sheets property, you should get an instance of Sheets that only returns Worksheet objects - in spite of the fact that the interface is capable of providing other types of sheets.

Even though this is quite well-documented, it might be one of the questions/answers that make so worthwhile.

On the other hand, the Worksheets interface only gives you worksheets - not charts.

The interfaces are not assignable to each other; therefore, you get the COM error. It's confusing - I'm not even sure if it's possible to get an instance of the Worksheets interface through the PIA - but that's Office Interop for ya.

_Workbook.Sheets gives you a Sheets instance. This interface gives you all types of sheets, not just worksheets; mainly, it includes charts, macro sheets, etc.

Note