Unable to Cast Com Object of Type 'Microsoft.Office.Interop.Excel.Applicationclass' to 'Microsoft.Office.Interop.Excel.Application'"

Unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'

If this is not a code-related error, then please remove the registry key listed below. This issue is caused by the key being left over from a higher version of Office.

Steps:

  1. Go to Start
  2. Select Run
  3. Type regedit and hit Enter
  4. Navigate to HKEY_CLASSES_ROOT -> TypeLib -> {00020813-0000-0000-C000-000000000046} -> 1.9/1.8/1.7
  5. Delete the registry key

Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.WorksheetClass'

Like Hans already said you need to create an Excel application first, then you add a Workbook to it, then you add a Worksheet (or try to access an already existing Sheets[1])

Give using Microsoft.Office.Interop.Excel and alias

using Excel = Microsoft.Office.Interop.Excel

and then

Excel.Application xlApp = new Excel.Application();
xlApp.Visible = true;
Excel.Workbook xlWb = xlApp.Workbooks.Add() as Excel.Workbook;
Excel.Worksheet xlSheet = xlWb.Sheets[1] as Excel.Worksheet;
Excel.Range range = xlSheet.get_Range("A1");

range.Value = "hello world!";

and then have a look at How to properly clean up Excel interop objects

C#: some of machines are throwing following error: Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass'

It seems some of contents are missing from the windows registry key,

and this can be happening when MS Excel is downgraded.

To fix this issue, Please go to regedit and delete [HKEY_CLASSES_ROOT-->TypeLib-->{00020813-0000-0000-C000-000000000046} --> 1.9]

and see if this works.

Registry



Related Topics



Leave a reply



Submit