无效的Cast Exception是未处理的Excel应用程序

时间:2018-03-02 06:52:39

标签: excel vb.net excel-interop

 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.任何想法如何解决这个错误? 先感谢您!

2 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:0)

如评论中所述,如果您只想启动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")