我正在尝试使用Office Developer Tools通过Visual Studio与Excel通信,但似乎无法将System._comobject对象强制转换为Microsoft.Office.Tools.Excel类型:
Private Sub ThisAddIn_Startup() Handles Me.Startup
Me.Application.Workbooks.Add()
Me.Application.Worksheets.Add()
Dim sheet2 = Me.Application.Worksheets.Item(1)
Dim cell = sheet2.range("A2")
cell.Value = sheet2.GetType.ToString()
Dim tmpSheet As Microsoft.Office.Tools.Excel.Worksheet = Me.Application.Worksheets.Item(1)
End Sub
当我运行此代码时,单元格A2包含" System._ComObject"但是在将其转换为Worksheet类型时会出现异常。例外是:
Unable to cast COM object of type 'System.__ComObject' to interface
type 'Microsoft.Office.Tools.Excel.Worksheet'. This operation failed
because the QueryInterface call on the COM component for the interface with
IID '{297DC8D9-EABD-45A1-BDEF-68AB67E5C3C3}' failed due to the following
error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
我知道这通常是因为不正确的注册码,例如: unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'"
但是我只有一个注册密钥,删除它,办公室本身把它放回原处,所以我猜它是正确的。
有谁知道如何解决这个问题? 我使用Visual Studio 2015和office 2013 Standard,英文版。 提前谢谢!
答案 0 :(得分:0)
显然我需要使用类型:
Microsoft.Office.Interop.Excel.Worksheet
而不是
Microsoft.Office.Tools.Excel.Worksheet