Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This is my first attempt to capture some data from excel from within one C# console application.

I get the error "unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'".

This code used the 'Microsoft Excel 12.0 Object Library' , and I gave reference to Microsoft.Office.Interop.Excel.

Still, I could not get over this error - which I believe has its own quick solution.

I digged a little bit this site and came across with this solution: Interop type cannot be embedded

However, I could not understand so could not implement what was suggested as a solution.

My .Net version is 4.5.

Any help is very much appreciated.

Thanks in advance.

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;  using Excel = Microsoft.Office.Interop.Excel;  namespace deneme { class Program {     static void Main(string[] args)     {          Excel.Application xlApp = new Excel.Application();         xlApp.Visible = true; // <-- excel application         xlApp.DisplayAlerts = false;          // Open the workbook.         Excel.Workbook wBook = xlApp.Workbooks.Open("C:\\FNN\\XLA\\fnnComTemplate.xlsx",             Type.Missing, Type.Missing, Type.Missing, Type.Missing,             Type.Missing, Type.Missing, Type.Missing, Type.Missing,             Type.Missing, Type.Missing, Type.Missing, Type.Missing,             Type.Missing, Type.Missing);          // get the sheet         Excel.Worksheet wSheet = wBook.Sheets[0];         // foreach (Excel.Worksheet sheet in wBook.Sheets) { if (sheet.Name == "templateSheet") { wSheet = sheet; } }          Excel.Range rng = wSheet.get_Range("A1");          aux = wSheet.Range["F6"].Value;          Console.WriteLine("interop result:" + aux);         Console.ReadLine();      }  } } 
like image 765
Aykut Saribiyik Avatar asked Jan 21 '15 11:01

Aykut Saribiyik


1 Answers

Microsoft Office 365

I was running Microsoft Office 365 with Windows 10 and tried the mentioned solutions to remove the registry key without success.

I went to the control panel in an attempt to repair the Office 365 suite. enter image description here

  1. Right Click on Start Menu
  2. Click Apps and Features
  3. Search Microsoft 365
  4. Click Microsoft 365
  5. Click Modify

I selected the quick repair enter image description here

Tried my program again and everything worked!

like image 77
Alex Avatar answered Sep 25 '22 05:09

Alex