VB: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)

1

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
excel
vb.net
office-interop
asked on Stack Overflow May 7, 2018 by Kuo-hsuan Hsu • edited May 7, 2018 by Cindy Meister

2 Answers

1

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

answered on Stack Overflow May 7, 2018 by Eugene Astafiev
0

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!


User contributions licensed under CC BY-SA 3.0