Domanda

Here is my code, but for some reason it is failing on the saveas line (object does not support, etc).

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
È stato utile?

Soluzione

In your code wb is Excel.Application object, rather than Excel.Workbook. And Excel.Application doesn't support SaveAs method. User this one instead:

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top