无法为 VSTO 加载项转换 'Excel.WorksheetClass' 类型的 COM 对象

Unable to cast COM object of type 'Excel.WorksheetClass' for VSTO add-in

我已经尝试了一些方法,但仍然出现此错误。我正在使用 VBA 和 Visual Studio 创建一个 excel VSTO。

我进行此设置的方式是功能区中有一个按钮,用户可以单击该按钮,它将浏览并取消列出所有表格。我想我的问题在于实际连接到 Excel 的当前活动实例以循环遍历工作表。

An exception of type 'System.InvalidCastException' occurred in ConvertAllTablesToRange.dll but was not handled in user code

Additional information: 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)).

这是我试过的方法:

Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click

    Dim wks As Worksheet, objList As ListObject

    For Each wks In Globals.ThisAddIn.Application.ActiveWorkbook  ' ERROR LINE

        For Each objList In wks.ListObjects
            objList.Unlist()
        Next objList

    Next wks
End Sub

Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
    Dim wks As Worksheet, objList As ListObject
    Dim exApp As Excel.Application

    exApp = Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets

    For Each wks In exApp.Worksheets
        For Each objList In wks.ListObjects
            objList.Unlist()
        Next objList
    Next wks
End Sub

两者都抛出相同的错误

您需要检查是否有工作表(不是什么都没有)

Dim NativeWorkbook As Microsoft.Office.Interop.Excel.Workbook =
Globals.ThisAddIn.Application.ActiveWorkbook
If NativeWorkbook IsNot Nothing Then
Dim vstoWorkbook As Microsoft.Office.Tools.Excel.Workbook =
    Globals.Factory.GetVstoObject(NativeWorkbook)
End If

查看更多信息:here msdn

EDIT:这是一个工作示例,但在 C# 中,而不是在 VB.NET 中,我认为你可以转换它,因为我在 [=21 中效率不高=]:

Microsoft.Office.Interop.Excel.Application XL =   Globals.ThisAddIn.Application;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook = default(Microsoft.Office.Interop.Excel.Workbook);
            xlWorkBook = XL.ActiveWorkbook;
            foreach (Microsoft.Office.Interop.Excel.Worksheet xs_loopVariable in xlWorkBook.Sheets)
            {
                    foreach (Microsoft.Office.Interop.Excel.ListObject objList in xs_loopVariable.ListObjects)
                    {
                     objList.Unlist();
                    }
            }

在普通香草 VBA 中,您可以根据自己的要求进行调整:

Worksheet 的引用来自 Worksheets 集合,因此代码块 #1 应该有:

For Each wks In Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets

在代码块 #2 中,将 Worksheets 集合分配给 Application 对象毫无意义 - 两种不同的、不兼容的类型。试试这个:

Dim exApp As Excel.Application

' VBA requires Set for assigning any object type - remove
' the Set keyword if not needed in your approach
Set exApp = Globals.ThisAddIn.Application

For Each wks In exApp.ActiveWorkbook.Worksheets

结果发现有3个类型转换错误。

1- wks 变量需要设置为 Excel.Worksheet 因为 WorksheetMicrosoft.Office.Tools.Execl.Worksheet 而我需要的类型是 Microsoft.Office.Interop.Excel.Sheet

2- objList 变量也是如此。需要 Excel.ListObject 而不是 ListObject

3- 我需要搜索 collection Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets

这会清除所有错误并使代码正常运行。