Had both 1.7 and 1.8 in registry. Resolved by removing:
HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.8
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();
}
}
}
If this is not a code related error than please remove below given key from registry.
Steps:
Start--> Run --> regedit --> HKEY_CLASSES_ROOT-->TypeLib-->{00020813-0000-0000-C000-000000000046} --> 1.8/1.7 (Delete this)
Solution: This issue can be resolved by deleting an invalid registry key left over from the higher version of Office. Follow the above steps.
Cause: This issue is caused by a left over registry key if you have downgraded your version of Microsoft Office from either Office 2010 to Office 2007, or Office 2013 to Office 2010 or 2007.
Let me know if this solution is not helped to you
I am sure the answers to this question (and similar questions) proposing editing the registry and removing old references to versions of Office no longer on the machine are correct.
I am only adding this answer because I am currently kicking myself. Two hours spent hunting keys in the registry and getting nowhere. Last throw of the dice was to try a simple repair on Microsoft Office and suddenly my error is gone.
... seems to me it's worth trying a repair before getting too deep into manually editing the registry.
A machine had been updated to Office 2016 64-bit and the COM interface was throwing exceptions when called from the a 32-bit application. It did not have old TypeLib entries like the ones in Jayesh's answer.
Comparing ProcessMonitor traces to a working machine with Office 2016 led to what look like dead end registry keys in
HKEY_CLASSES_ROOT\Wow6432Node\TypeLib\{00024..
The working machine would check for the non-existent registry key and return
NAME NOT FOUND
The failing machine had the registry key but then would throw an exception shortly thereafter.
After repeatedly deleting the registry key, rerunning the trace, deleting the next failing key, the COM interface functionality was restored.
Had both 1.7 and 1.8 in registry. Resolved by removing:
HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.8
Using the exact same code I got the same problem in a laptop when everything worked fine on another one. I tried various solutions found here and there in the internet but what finally worked, was the explicit definition of the use of non 32 bit version of the library. For doing so in visual studio, I had to tick and then untick the box "Prefer 32-bit" in the build section of the project configuration, which added <Prefer32Bit>false</Prefer32Bit>
in the .csproj file.
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
来源:https://stackoverflow.com/questions/28066719/unable-to-cast-com-object-of-type-microsoft-office-interop-excel-applicationcla