Reputation: 243
Dim app, fso, file, fName, wb, dir
dir = "D:\TA"
dirsave = "D:\TA\XLS"
Set app = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
For Each file In fso.GetFolder(dir).Files
If LCase(fso.GetExtensionName(file)) = "csv" Then
fName = fso.GetBaseName(file)
Set wb = app.Workbooks.Open(file)
app.Application.Visible = True
app.Application.DisplayAlerts = False
app.ActiveWorkbook.SaveAs dirsave & fName & ".xls", 43
app.ActiveWorkbook.Close
app.Application.DisplayAlerts = True
app.Application.Quit
End if
Next
Set fso = Nothing
Set wb = Nothing
Set app = Nothing
wScript.Quit
I am using above VB script to open csv file and save it as xls file but it throws error
Error : SaveAs method of Workbook class failed.
How can I fix it? Thanks in advance
Upvotes: 0
Views: 19492
Reputation: 1886
It looks like you are missing a "\" in the script. It probably should be:
dirsave = "D:\TA\XLS\"
or alternatively, it could also be:
app.ActiveWorkbook.SaveAs dirsave & "\" & fName & ".xls", 43
Either option should work, although I would prefer option one, as it is neater
Upvotes: 1