1
votes

I am working on an app which needs to import data from Excel.

My solution is using Microsoft.office.Interop.Excel.

But I get this error when I debug:

Message=Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' 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. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY))

Below is my code:

Imports Excel = Microsoft.office.Interop.Excel

Private Sub BExcel1_Click(sender As Object, e As EventArgs) Handles BExcel1.Click
    OpenFileDialog1.Filter = "Excel Files|*.xlsx; *.xls; *.xlsm"
    If (OpenFileDialog1.ShowDialog() = DialogResult.OK) Then
        ExcelPath1.Text = OpenFileDialog1.FileName
    End If

    Dim XlApp As New Excel.Application
    Dim XlWorkBook As Excel.Workbook
    Dim XlWorkSheet As Excel.Worksheet
    XlWorkBook = XlApp.Workbooks.Open(ExcelPath1.Text)

 End Sub

I have googled to find some solutions (as following), but they didn't work out:

  1. I have repaired Office
  2. I have repaired Visual Studio
  3. I have used the registry editor to check the Computer\HKEY_CLASSES_ROOT\TypeLib\, but under the 00020813-0000-0000-C000-000000000046, I got only one version 1.9, it seems not like the conflict between two version?

Any ideas to fix this problem?

VS version:2017 community

Excel version:2016

Microsoft.Office.Interop.Excel version:15.0.0.0
2
Repairing office is not good enough, re-install it. Run chkdsk.exe before you do so to verify that the disk drive is still in decent shape. - Hans Passant
Excel 2016 should be showing version 16.#, not 15, which is Office 2013. Where are you seeing these conflicting version numbers? - Cindy Meister
@Cindy the version of Microsoft.Office.Interop.Excel is from the Reference Properties of Visual Studio Property Manager. It shows 15.0.0.0. And the key 00020813-0000-0000-C000-000000000046 under the Computer\HKEY_CLASSES_ROOT\TypeLib\, it got only sub key 1.9 (Computer\HKEY_CLASSES_ROOT\WOW6432Node\TypeLib\{00020813-0000-0000-C000-000000000046}\1.9) ,under the Default tag : Data: Microsoft Excel 16.0 Object Library). I have not seen any conflicting version numbers, so I am confuse. - Kuo-hsuan Hsu
Which version of Excel is installed on your machine? - Cindy Meister
@Cindy Office 2016 standard. - Kuo-hsuan Hsu

2 Answers

1
votes

First of all, try to run Visual Studio with the /ResetUserData command line argument. Read more about that in the Error "Unable to cast COM object..." when exporting to Microsoft Excel from Team Explorer 2008 article.

Obviously you are trying to connect to a wrong Excel version. Looks like you have some extra windows registry keys left after uninstalling an old version of Office or vice versa. Anyway, take a look at the How to solve “Unable to cast COM object of type Microsoft.Office.Interop.Excel.ApplicationClass’ to interface type ‘Microsoft.Office.Interop.Excel._Application’” blog post which describes exactly the same issue. Basically you need to find a wrong entry in the windows registry and then delete it.

BTW When you add a new COM reference to the project a missed PIA is generated automatically (if it doesn't exist any longer). So, that is a possible way to go. Also you may try to embed interop types into your own assembly like the following screenshot shows:

enter image description here

0
votes

If you set the Embed Interop Types property to True (since .NET Framework 4.0) and the Specific Version property to False for the Office Interop assembly reference, then your code should work with any version of Excel.

However, you must compile the code with the same bitness as your Office. For a 32-bit version of Office, you must compile your code as x86, otherwise this exception can still occur on a 64-bit machine!