Checking to see if SaveAs was successful VBA

狂风中的少年 提交于 2019-12-11 04:39:24

问题


What kind of statement do I need to check whether or not a SaveAs operation was successful in vba?

Sub saveBookAs()
    wb.SaveAs fileName:="newFile"

End Sub

回答1:


You don't need a statement to check if the workbook was saved or not :) If the Save As process fails then that line will error out automatically :)

Having said that if you want to check if a file exists or not you can always use the DIR function. DIR Returns a String representing the name of a file, directory, or folder that matches a specified pattern or file attribute, or the volume label of a drive.

Syntax

Dir[(pathname[, attributes])]

Check VBA help for more details.

EDIT

Another tip.

If you don't want your code to break, then you can also use proper Error Handling.

For example

Sub Sample()
    On Error GoTo Whoa

    ActiveWorkbook.Save SomeFilePathAndName

    Exit Sub
Whoa:
    MsgBox Err.Description
End Sub


来源:https://stackoverflow.com/questions/20228409/checking-to-see-if-saveas-was-successful-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!