错误:SaveAs Workbook类的方法失败

时间:2016-04-08 11:13:00

标签: excel vbscript

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

我使用上面的VB脚本打开csv文件并将其保存为xls文件,但它会抛出错误

错误:另存为Workbook类的方法失败。

我该如何解决? 提前致谢

1 个答案:

答案 0 :(得分:0)

看起来你在脚本中缺少“\”。它可能应该是:

dirsave = "D:\TA\XLS\"

或者也可以是:

app.ActiveWorkbook.SaveAs dirsave & "\" & fName & ".xls", 43

任何一个选项都应该有用,虽然我更喜欢选项一,因为它更整洁