3

using the Microsoft.Office.Interop.Excel.dll leads to the following error:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll

Additional information: Retrieving the COM class factory for component with CLSID {00020819-0000-0000-C000-000000000046} failed due to the following error: 80040154 Klasse nicht registriert (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

The error occures in the fourth line of this code:

 using EX = Microsoft.Office.Interop.Excel;

 private void LoopBANFDokumenteLibrary(System.Uri Link)
 {
        EX.Application MSExcel = new EX.Application();
        EX.Workbook WB = MSExcel.Workbooks.Add(new EX.Workbook()); //ERROR: Exception thrown
        EX.Worksheet WS = WB.Worksheets.Add(new EX.Worksheet());
        /*...and further code...*/
 }

Some years ago, I used this Interop.Excel.DLL daily and a never had this problem. Opening regedit.exe, the dll is registered as you can see on this screenshot:

regedit

Does anybody has an idea, whats wrong?

Regards and thank you, Jan

Community
  • 1
  • 1
Jan021981
  • 521
  • 3
  • 28
  • That error generall indicates Excel isn't installed... – stuartd Jun 02 '17 at 12:56
  • I'm currently running it :-) – Jan021981 Jun 02 '17 at 12:58
  • .. could it be a version mismatch? – stuartd Jun 02 '17 at 12:58
  • `EX.Workbook WB = MSExcel.Workbooks.Add(Missing.Value);` and ditto for adding the WorkSheet, passing `new EX.XXX()` is not valid, there is no such overload in the interface. – Alex K. Jun 02 '17 at 12:58
  • This error can also occur when your application is of a different "bitness" than your Office installation. When you're using 32bit Office then your application must be compiled / run as 32bit, otherwise when you're using the 64bit Office then your application must be compiled / run as 64bit. – bassfader Jun 02 '17 at 13:07
  • Related (but not sure it's a duplicate): https://stackoverflow.com/q/13660938/3775798 – Knowledge Cube Jun 02 '17 at 13:09
  • Try : EX.Workbook WB = (_Workbook)MSExcel.Workbooks.AddXlWBATemplate.xlWBATWorksheet); – Demon Jun 02 '17 at 13:38

1 Answers1

1

Change

EX.Workbook WB = MSExcel.Workbooks.Add(new EX.Workbook());

to

EX.Workbook WB = MSExcel.Workbooks.Add();

Similarly use

EX.Worksheet WS = WB.Worksheets.Add();
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250