简体   繁体   中英

Error : SaveAs method of Workbook class failed

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM