MS Access将.csv转换为.xls

时间:2014-04-16 15:31:48

标签: excel vba ms-access csv xls

这是我的代码,但由于某种原因,它在saveas行上失败(对象不支持等)。

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

1 个答案:

答案 0 :(得分: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