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

13,286

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

Share:
13,286
Kuo-hsuan Hsu
Author by

Kuo-hsuan Hsu

.Net app developer, experienced with MySQL, MSSQL, PHP. Now, my team has a new project which would relate to WebGL and Javascript. Personal hobby: Arduino is fun and trying to build some device. My LinkedLn https://www.linkedin.com/in/gordon-hsu-a235936b/ open to have any remote-development job~

Updated on June 28, 2022

Comments

  • Kuo-hsuan Hsu
    Kuo-hsuan Hsu almost 2 years

    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