1

I have been able to open an excel workbook using a console app in Visual Studio 2019 with the following code:

 using Excel = Microsoft.Office.Interop.Excel;
 private void OpenXL2()
    {
        Excel.Application excelApp = new Excel.Application();
        if (excelApp == null)
        {
            return;
        }

        // open an existing workbook

        string workbookPath = @"C:\Illustrator\InvoiceTemplates\invtemplate.xls";
        Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath);

        excelApp.Visible = true;

        // get all sheets in workbook
        Excel.Sheets excelSheets = excelWorkbook.Worksheets;

        // get some sheet
        string currentSheet = "TimeSheet";
        Excel.Worksheet excelWorksheet =
             (Excel.Worksheet)excelSheets.get_Item(currentSheet);

    }

but when I try to use the same code in a windows form app the following error occurs when the excelApp.Workbooks.Open(workbookPath) is reached:

System.InvalidCastException: 'Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).'

I have added a reference to the "Microsoft Excel 16.0 Object Library"

Thanks.

stuartd
  • 70,509
  • 14
  • 132
  • 163
philb39956
  • 11
  • 2
  • 1
    Do you actually need to use Interop at all here? Interop opens the entire Excel application. If you just want to work with the data inside the file, then you could use another library for that (there are several free ones available via Nuget packages), and it would be a lot more reliable. – ADyson Nov 15 '20 at 23:11
  • Try running a quick repair on Office https://stackoverflow.com/questions/28066719/unable-to-cast-com-object-of-type-microsoft-office-interop-excel-applicationcla – Michael74 Nov 15 '20 at 23:52
  • Thanks for the responses. I did do a repair of the excel install and the issue is still there. – philb39956 Nov 16 '20 at 00:05
  • The thing that is baffling to me is that I am able to open an excel workbook with a console app and I just successfully open one with a VB.net windows form app. – philb39956 Nov 16 '20 at 00:24
  • Console app is which .NET Framework / Core version? What about winforms app? – mjwills Nov 16 '20 at 01:13
  • Does https://social.msdn.microsoft.com/Forums/sqlserver/en-US/5807c42c-e82a-449b-9fd0-1b7bea4b5f95/how-to-fix-e-systeminvalidcastexception-unable-to-cast-com-object-of?forum=exceldev or https://stackoverflow.com/questions/41562540/unable-to-cast-com-object-of-type-microsoft-office-interop-excel-applicationcla help? – mjwills Nov 16 '20 at 01:14
  • The console app and the winforms app are created in Visual Studio 2019 and are .NET. – philb39956 Nov 16 '20 at 02:43
  • I'm now going to do another repair of my microsoft office, but us the more thorough internet based repair to see if the problem gets solved. – philb39956 Nov 16 '20 at 02:45
  • Try installing Microsoft.Office.Interop.Excel through Nuget and then run the same function. I could successfully run the same exact code provided here . –  Nov 16 '20 at 03:21
  • 1
    `The console app and the winforms app are created in Visual Studio 2019 and are .NET.` Read my question again. – mjwills Nov 16 '20 at 03:57
  • 1
    In response to mjwills: Thank you for pointing me in the right direction! The .NET /Core version for the console app is 3.1 and was 4.7.2 for the windows forms app. I set the .NET version for the windows forms app to 4.5.1 and the app ran successfully! I then reset the it back to 4.7.2 and it is running fine. – philb39956 Nov 16 '20 at 14:38

1 Answers1

1

try like that

Type excel_type = Type.GetTypeFromProgID("Excel.Application");
        dynamic excel_obj = Activator.CreateInstance(excel_type);
        var workbook = excel_obj.Workbooks.Open(Filename: filePath, ReadOnly: false);
Shaybakov
  • 618
  • 7
  • 9