Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.Application'.

Anonymous
2024-04-22T12:42:52+00:00

Hi,

I'm trying to make a simple vb code with a button to open excel (with macro) in visual studio, as you can see below:

Imports xls = Microsoft.Office.Interop.Excel 

Public Class Form1

    Private appXls As xls.Application 

    Private sheet As xls.Worksheet 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        appXls = New xls.Application

        appXls.Visible = True  

        appXls.Workbooks.Open("C:\Users...\file.xlsm")

    End Sub

End Class

and I get this error message:

System.InvalidCastException : '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: Interface non enregistrée (0x80040155).'

I use excel 2019 version 2308(build 16731.20636

and i am on the IDE of visual studio and i am on the .NET 8.0 version

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

and I find the two REG_SZ below:

By default: Microsoft Excel 16.0 Object Library

PrimaryInteropAssemblyName: Microsoft.Office.Interop.Excel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\ClickToRun\AppVMachineRegistryStore\Integration\Ownership\SOFTWARE\Classes\Interface{000208D5-0000-0000-C000-000000000046}

and I find the two REG_SZ below:

by default (undefined value)

{@@}AppVCreatedKey: (litteraly nothing that's white it's not saying undefined value)

I can't execute the repair of excel in settings -->application -->modify because modify is grey.

I also can't uninstall excel and reinstall it because it's a second-hand laptop and excel was already there, so I don't have the key.

I am on an internship. I hope that with all this information someone will be able to help me. It makes me cry and mad.

Microsoft 365 and Office | Excel | Other | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Anonymous
    2024-04-22T14:25:45+00:00

    Hi Than_seji!

    I am Shakiru, an independent advisor and a user like you, and I am glad to be helping you out today.

    The error you’re encountering is typically related to the COM registration of the Excel application not being correct or not matching the expected interface.

    • Make sure that the version of the Microsoft.Office.Interop.Excel assembly referenced in your project matches the version of Excel installed on your machine. Since you’re using Excel 2019, you might need to update the reference to the appropriate version if it’s currently set to an older version.
    • You may need to manually register the Excel interop assemblies using regasm.exe. This tool can be found in the .NET Framework installation directory. You’ll need to run it with administrator privileges.
    • Verify that your Visual Studio project’s configuration is set to the correct platform target (x86 or x64) that matches your Office installation.

    Please try this to check the assembly version in your project:

    Imports System.Reflection

    Public Sub CheckAssemblyVersion() Dim excelAssembly As Assembly = Assembly.Load("Microsoft.Office.Interop.Excel") Console.WriteLine($"Assembly version: {excelAssembly.GetName(). Version}") End Sub

    Please let me know how it goes and if you need any other assistance and I will be more than happy to help further.

    Thank you

    Best Regards, Shakiru

    0 comments No comments
  2. Anonymous
    2024-04-22T20:02:54+00:00

    Hi Shakiru,

    I finally find my way out of this problem about 10 minutes ago. I looked for the quick repair for excel but in my installed apps there was no application called excel but one called microsoft 365... (why group those applications into one?) so I finally did the quick repair and it works thanks for your help sorry for the time it took you.

    Regards,

    Than

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2024-04-22T20:18:46+00:00

    Hi Than_seji!

    I'm glad that works :-)

    Thank you

    Best Regards, Shakiru

    0 comments No comments