c8irish
c8irish

Reputation: 155

Getting 'Application' does not contain a definition for 'Workbooks' when working with Interop.Excel

Built this program like in the last 3 months. It is just a desktop app they wanted to read through and Excel file and build some SQL with the data. Was working fine and another team added to it to get some data they needed. I moved to a new team and haven't opened this project for like a month.

Since I know how it works, I was asked to add a piece to the program and when I opened it, the Interop.Office part I was using to open and go through the Excel is now giving me errors.

The two errors are:

'Application' does not contain a definition for 'Workbooks'

and

'Workbook' does not contain a definition for 'Sheets'.

I have gone through what has been changed in the program and nothing should have effected this. I even went into branches that I know were working when I last opened it and they don't work as well.

The code where the errors come in is here:

Excel.Application xlApp = new Excel.Application();  
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filePath);
Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;

The errors are on the second and third lines.

The using statement is this:

using Excel = Microsoft.Office.Interop.Excel;

I have spent a day and a half searching for an answer without avail. The package is currently a COM reference but I did add it as a project reference instead and that caused a bunch of different errors so I went back to the COM reference. I did have some work security updates since I have last worked on this but I don't know what they were to know if they could even possibly effect this.

Upvotes: 1

Views: 307

Answers (1)

Hayden Beckwith
Hayden Beckwith

Reputation: 1

I was seeing the same issue despite COM references being set up correctly, blowing away/redownloading dll file, etc. Then I found a similar issue online that outlined a way to uninstall/reinstall Office 365. Following these steps resolved the issue for me:

Open your Control Panel (Start Button on Desktop, then click the Settings gear icon), the select the Apps category and scroll down until you see 'Microsoft 365 Apps'. Click once on Microsoft 365 Apps and then select "Modify". I recommend a "Online Repair".

https://learn.microsoft.com/en-us/answers/questions/258475/unable-to-cast-com-object-of-type-microsoft-office

Upvotes: 0

Related Questions