vb.net VB:加载类型库/DLL 时出错。(来自 HRESULT 的异常:0x80029C4A(TYPE_E_CANTLOADLIBRARY)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50210215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)
提问by Kuo-hsuan Hsu
I am working on an app which needs to import data from Excel.
我正在开发一个需要从 Excel 导入数据的应用程序。
My solution is using Microsoft.office.Interop.Excel.
我的解决方案是使用 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))
Message=无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象转换为接口类型“Microsoft.Office.Interop.Excel._Application”。此操作失败,因为 IID 为“{000208D5-0000-0000-C000-000000000046}”的接口的 COM 组件上的 QueryInterface 调用因以下错误而失败:加载类型库/DLL 时出错。(来自 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:
我用谷歌搜索找到了一些解决方案(如下),但他们没有解决:
- I have repaired Office
- I have repaired Visual Studio
- I have used the registry editor to check the
Computer\HKEY_CLASSES_ROOT\TypeLib\, but under the00020813-0000-0000-C000-000000000046, I got only one version 1.9, it seems not like the conflict between two version?
- 我已经修复了 Office
- 我已经修复了 Visual Studio
- 我用注册表编辑器检查过
Computer\HKEY_CLASSES_ROOT\TypeLib\,但是在 下00020813-0000-0000-C000-000000000046,我只有一个1.9版本,这两个版本之间似乎没有冲突?
Any ideas to fix this problem?
任何想法来解决这个问题?
VS version:2017 community
Excel version:2016
Microsoft.Office.Interop.Excel version:15.0.0.0
采纳答案by Eugene Astafiev
First of all, try to run Visual Studio with the /ResetUserDatacommand line argument. Read more about that in the Error "Unable to cast COM object..." when exporting to Microsoft Excel from Team Explorer 2008article.
首先,尝试使用/ResetUserData命令行参数运行 Visual Studio 。在“从 Team Explorer 2008 导出到 Microsoft Excel 时出现的错误”一文中阅读有关此内容的更多信息。
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.
显然,您正在尝试连接到错误的 Excel 版本。卸载旧版本的 Office 后,您似乎还剩下一些额外的 Windows 注册表项,反之亦然。无论如何,请查看如何解决“无法将 Microsoft.Office.Interop.Excel.ApplicationClass 类型的 COM 对象转换为接口类型 'Microsoft.Office.Interop.Excel._Application'”博客文章,其中描述了完全相同问题。基本上,您需要在 Windows 注册表中找到错误的条目,然后将其删除。
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:
顺便说一句,当您向项目添加新的 COM 引用时,会自动生成丢失的 PIA(如果它不再存在)。所以,这是一种可能的方式。您也可以尝试将互操作类型嵌入到您自己的程序集中,如下面的屏幕截图所示:
回答by Olivier Jacot-Descombes
If you set the Embed Interop Typesproperty to True(since .NET Framework 4.0) and the Specific Versionproperty to Falsefor the Office Interop assembly reference, then your code should work with any version of Excel.
如果您将Embed Interop Types属性设置为True(自 .NET Framework 4.0 起)并将Specific Version属性设置False为 Office Interop 程序集引用,则您的代码应该适用于任何版本的 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!
但是,您必须以与您的 Office 相同的位数编译代码。对于 32 位版本的 Office,您必须将代码编译为x86,否则在 64 位机器上仍然会出现此异常!


