2014-04-16 129 views
1

這是我的代碼,但由於某些原因,它在saveas行上失敗(對象不支持等)。MS Access將.csv轉換爲.xls

Sub convertToXLS() 
    Dim wb As Object 
    Set wb = CreateObject("Excel.Application") 
    Dim strFile As String 
    strFile = "C:\path to my file\filename.csv" 

    wb.Workbooks.Open (strFile) 
    With wb 
     .SaveAs FileName:=Replace(strFile, ".csv", ".xls") 
     .Close True 
    End With 
    Set wb = Nothing 

End Sub 

回答

2

在你的代碼wbExcel.Application對象,而不是Excel.WorkbookExcel.Application不支持SaveAs方法。用戶這一塊,而不是:

Sub convertToXLS() 
    Dim xlApp As Object 
    Dim wb As Object 
    Dim strFile As String 

    Set xlApp = CreateObject("Excel.Application")   
    strFile = "C:\path to my file\filename.csv"  
    Set wb = xlApp.Workbooks.Open(strFile) 

    With wb 
     ' where 56 is value of excel constant xlExcel8 
     .SaveAs FileName:=Replace(strFile, ".csv", ".xls"), FileFormat:=56 
     .Close True 
    End With 
    'clean up 
    Set wb = Nothing 
    xlApp.Quit 
    Set xlApp = Nothing 
End Sub 
+0

它的工作,但是當我嘗試打開的.xls,它說,這是比文件擴展名 – MyNameIsKhan

+0

是啊,你應該檢查我的更新答案不同的格式,使用'.SaveAs文件名: =替換(strFile,「.csv」,「.xls」),FileFormat:= 56' –

+0

沒關係,錯過了編輯。該文件格式的東西解決了這個問題。什麼是fileformat 56? .xls格式? – MyNameIsKhan