Question

I am running this code in a separate project and it works no issue, but for some reason in this project it throws a compile error. What is incorrect with this statement:

private static Excel.Application docExcel;
docExcel.Sheets[wsCount + 1].Select();

Error:

Object does not contain a definition for 'Select'

Image of installed references:

enter image description here

Was it helpful?

Solution

I assume you're trying to call the Select method on a Worksheet and not the Linq Select method or some other extension method.

The indexer for the Sheets collection returns an object since it can contain many different types of wheets (worksheets, charts, etc.) and there's not a common base type between those classes.

If you know that you are selecting a worksheet, use the Worksheets property instead and explicitly cast it:

private static Excel.Application docExcel;
((Excel.Worksheets)docExcel.Worksheets[wsCount + 1]).Select();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top