未处理无效的 Cast 异常 Excel 应用程序

Invalid Cast Exception was unhandled Excel Application

 Dim oExcel As Excel.ApplicationClass = New Excel.ApplicationClass
            Dim objWS As New Microsoft.Office.Interop.Excel.Worksheet
            Dim oBook As Excel.WorkbookClass
            Dim oBooks As Excel.WorkbookClass

            'Start Excel and open the workbook.
            oExcel = CreateObject("Excel.Application")
            oExcel.Visible = True
            oBooks = oExcel.Workbooks
            oBook = oBooks.Open("H:\Copy of Book1.xlsm")

您好!我在 oExcel = CreateObject("Excel.Application")

处出现无效转换异常错误

我正在使用 Visual Basic,我正在尝试打开名为 Copy of Book1 的 excel 文件。我也在使用 Microsoft Excel 2010。知道如何解决该错误吗? 先感谢您!

试试这个:

Sub openExcel()
Dim objExcel As Excel.Application
Set objExcel = CreateObject("Excel.Application")
Dim ws As Worksheet
Dim wb As Workbook

objExcel.Visible = True
Set wb = objExcel.Workbooks.Open("H:\Copy of Book1.xlsm")
End Sub

如评论中所述,如果您只想启动 Excel 并打开文件,请按如下方式更改代码

Dim oExcel As Excel.Application = New Excel.Application
Dim oBook As Excel.Workbook

'Start Excel and open the workbook.    
oExcel.Visible = True
oBook = oExcel.Workbooks.Open("H:\Copy of Book1.xlsm")