我的VB6.0项目上提示运行时错误'91'

时间:2018-12-02 05:38:15

标签: excel vb6

让我知道我的代码在哪里错误:

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

1 个答案:

答案 0 :(得分:0)

您的代码存在多个问题。

您永远不会设置Application对象。如果未打开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之前,在代码中没有看到您将对象分配给Label4.Caption的地方。 Label1Label2也是如此。