我的 VB6.0 项目提示运行时错误“91”
Runtime error '91' is prompting on my VB6.0 project
让我知道我的代码哪里出错了:
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
**Set oBook = oExcel.Application.Workbooks.Open(App.Path & "\Data" & "\" & Label4.Caption & "" & "\" & Label4.Caption & ".xls")**
'opens the filename workbook but this is where the ERROR pointing me at.
Set oSheet = oExcel.Application.ActiveSheet 'activate the first worksheet
oSheet.Range("A1").Value = Label1.Caption
oSheet.Range("B1").Value = Label2.Caption
oExcel.Save
oExcel.Quit
您的代码存在多个问题。
您从未设置您的应用程序对象。如果Excel没有打开,那么需要添加:
Set oExcel = CreateObject("Excel.Application")
以Set ... = oExcel.Application
开头的行可以简化为
Set ... = oExcel ' (no .Application)
因为oExcel
是你的应用级对象
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(App.Path & "\Data" & "\" & _
Label4.Caption & "" & "\" & Label4.Caption & ".xls")
Set oSheet = oExcel.ActiveSheet 'activate the first worksheet
oSheet.Range("A1").Value = Label1.Caption
oSheet.Range("B1").Value = Label2.Caption
oExcel.Save
oExcel.Quit
然后,不清楚Label4
是什么。在使用 Label4.Caption
之前,我没有在您的代码中看到任何地方将对象分配给 Label4
。这也适用于 Label1
和 Label2
.
让我知道我的代码哪里出错了:
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
**Set oBook = oExcel.Application.Workbooks.Open(App.Path & "\Data" & "\" & Label4.Caption & "" & "\" & Label4.Caption & ".xls")**
'opens the filename workbook but this is where the ERROR pointing me at.
Set oSheet = oExcel.Application.ActiveSheet 'activate the first worksheet
oSheet.Range("A1").Value = Label1.Caption
oSheet.Range("B1").Value = Label2.Caption
oExcel.Save
oExcel.Quit
您的代码存在多个问题。
您从未设置您的应用程序对象。如果Excel没有打开,那么需要添加:
Set oExcel = CreateObject("Excel.Application")
以Set ... = oExcel.Application
开头的行可以简化为
Set ... = oExcel ' (no .Application)
因为oExcel
是你的应用级对象
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(App.Path & "\Data" & "\" & _
Label4.Caption & "" & "\" & Label4.Caption & ".xls")
Set oSheet = oExcel.ActiveSheet 'activate the first worksheet
oSheet.Range("A1").Value = Label1.Caption
oSheet.Range("B1").Value = Label2.Caption
oExcel.Save
oExcel.Quit
然后,不清楚Label4
是什么。在使用 Label4.Caption
之前,我没有在您的代码中看到任何地方将对象分配给 Label4
。这也适用于 Label1
和 Label2
.