让我知道我的代码在哪里错误:
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
答案 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
的地方。 Label1
和Label2
也是如此。