我试图使用以下代码将数据写入excel文件
Dim objexcel As Excel.Application Dim wbexcel As Excel.Workbook Dim wbExists As Boolean Set objexcel = CreateObject("excel.Application") objexcel.Visible = True On Error GoTo Openwb wbExists = False Set wbexcel = objexcel.Documents.Open("C:\Documents and Settings\TAYYAPP\Desktop\test folder\ERROR REPORT2.xls") wbExists = True Openwb: On Error GoTo 0 If Not wbExists Then Set wbexcel = objexcel.Workbook.Add End If
但我得到了一个
运行时错误对象不支持属性或方法
在线
Set wbexcel = objexcel.Workbook.Add
我引用了Excel对象库.
您需要更改此行:
Set wbexcel = objexcel.WorkBooks.Open( _ "C:\Documents and Settings\TAYYAPP\Desktop\test folder\ERROR REPORT2.xls")
注意WorkBooks,而不是Documents
至于此行设置wbexcel = objexcel.Workbook.Add,wbexcel被定义为工作簿,但该行是一个动作,因此:
objexcel.Workbooks.Add Set wbexcel = objexcel.ActiveWorkbook
编辑:除此之外,DoCmd.Transferspreadsheet可能是从Access到Excel传输一组数据(查询,表)的最简单方法.