Aykut Saribiyik
Aykut Saribiyik

Reputation: 845

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

I am attempting to capture some data from Excel from within a 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 included a reference to 'Microsoft.Office.Interop.Excel'.

I dug a little bit this site and came across Interop type cannot be embedded, but I could not understand it enough to implement what was suggested as a solution.

My .NET version is 4.5.

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();

    }

}
}

Upvotes: 51

Views: 174516

Answers (9)

Shohrat Suleymanov
Shohrat Suleymanov

Reputation: 61

Start->Run->Regedit find this path HKEY_CLASSES_ROOT\TypeLib{00020813-0000-0000-C000-000000000046} and remove it and repair your office packet

Upvotes: 1

meir
meir

Reputation: 926

I had this issue in PowerShell. Along with the mentioned issue, $xlApp.Workbooks returned null for me. Running the PowerShell script in 32-bit mode resolved my issue.

Upvotes: 1

DartFNM
DartFNM

Reputation: 81

My setup: Visual Studio: 2019, Office: 2019 x64, Target Framework: net5.0-windows

Nothing help me from error:

Unable to cast COM object of type 'System.__ComObject' 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: Error loading type library/DLL. (0x80029C4A (TYPE_E_CANTLOADLIBRARY)).

So I found via Sysinternals Process Monitor a missing file path for Excel.exe in the Registry for Win32 and changed it to the existing x64 version:

[HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.9\0\win32]
@="C:\\Program Files\\Microsoft Office\\Root\\Office16\\EXCEL.EXE"

Upvotes: 2

hawbsl
hawbsl

Reputation: 16003

A simple repair of Microsoft Office resolved the issue for me.

Upvotes: 11

Jayesh Goyani
Jayesh Goyani

Reputation: 11154

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

Upvotes: 49

Alex
Alex

Reputation: 4938

For Microsoft Office 365 in Windows 10

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

I went to Settings -> Apps in an attempt to repair the Office 365 suite:

Screenshot of the Microsoft Office 365 entry in Apps & features in the Windows 10+ Settings menu, with the mouse hovering over/clicking on the "Modify" button

To get there you can follow these steps:

  1. Open Start
  2. Select Settings
  3. Select Apps
  4. In the default page, Apps & features, search for Microsoft 365
  5. Select "Microsoft 365"
  6. Select "Modify"

I selected the Quick Repair option:

Screenshot of the Office Modify wizard with the 'Quick Repair' option selected

This resolved the issue for me.

Upvotes: 83

user8124226
user8124226

Reputation: 104

In my case I delete regedit in:

HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}

delete 1.9 enter image description here

and problem solved.

Upvotes: 0

Stathis Agrapidis
Stathis Agrapidis

Reputation: 61

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.

Upvotes: 4

bds
bds

Reputation: 188

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.

Upvotes: 2

Related Questions