2008-11-11 25 views
1

我嘗試使用下面的代碼將數據寫入Excel文件如何從訪問數據寫入到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對象庫。

回答

6

你需要改變這一行:

Set wbexcel = objexcel.WorkBooks.Open(_ 
    "C:\Documents and Settings\TAYYAPP\Desktop\test folder\ERROR REPORT2.xls")  

注意工作簿,沒有文件

至於此行集wbexcel = objexcel.Workbook.Add,wbexcel被定義爲一個工作簿,但該線是一個動作,所以:

objexcel.Workbooks.Add 
Set wbexcel = objexcel.ActiveWorkbook 

編輯: 順便說一句,DoCmd.Transferspreadsheet可能是傳輸一組數據的(最簡單的方法查詢,表)從Access到Excel。

+0

感謝該做的工作 也寫入數據,然後 wbexcel.insert「數據」 是這句法OK – tksy 2008-11-11 12:37:52

0

我有這個代碼的正常工作

Dim objexcel As Excel.Application 
        Dim wbexcel As Excel.Workbook 
        Dim wbExists As Boolean 
        Dim objSht As Excel.Worksheet 
        Dim objRange As Excel.Range 


        Set objexcel = CreateObject("excel.Application") 
        objexcel.Visible = True 
        On Error GoTo Openwb 
        wbExists = False 
        Set wbexcel = objexcel.Workbooks.Open("C:\Documents and Settings\TAYYAPP\Desktop\test folder\reports\ERROR REPORT2.xls") 
        Set objSht = wbexcel.Worksheets("Sheet1") 
        objSht.Activate 
        wbExists = True 
Openwb: 

        On Error GoTo 0 
        If Not wbExists Then 
        objexcel.Workbooks.Add 
        Set wbexcel = objexcel.ActiveWorkbook 
        Set objSht = wbexcel.Worksheets("Sheet1") 

        End If 

,但我想補充一個檢查,如果存在該文件我想看看它填充了值,如果是的話我想了下一組值將從最後填充。截至目前它覆蓋現有的值