使用 Visual Studio 2013 创建一个涉及 excel 的新程序。之前在早期的 excel 版本中完成了此操作,但现在无法使其工作。尝试创建 excel 应用程序对象会引发错误(但是当我查看任务管理器时会启动 excel 进程)。我们将 Office 365 与 Excel 2016 一起使用。我已卸载 Office 并进行了全新安装,但仍然出现相同的错误。
有一个 vb .net windows 项目。我已经通过选择 COM - 类型库 - 添加了一个引用,并添加了 Microsoft Excel 16.0 对象库(版本 1.9)。
代码是这样的:
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim appXL As Excel.Application
Dim wbXl As Excel.Workbook
Dim shXL As Excel.Worksheet
Dim raXL As Excel.Range
' Start Excel and get Application object.
appXL = CreateObject("Excel.Application")
appXL.Visible = True
appXL = CreateObject("Excel.Application")
抛出错误:
An unhandled exception of type 'System.InvalidCastException' occurred in WindowsApplication3.exe
Additional information: 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 not registered (Exception from HRESULT: 0x80040155).
I have checked registry editor in HKEY_CLASSES_ROOT - typelib and found the IID {000208D5-0000-0000-C000-000000000046}. It looks like this:
TypeLib (Default) REG_SZ {00020813-0000-0000-C000- 000000000046}
Version REG_SZ 1.9
任何帮助都感激不尽。谢谢。