VBA 询问用户是否要保存工作簿?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18411382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:56:58  来源:igfitidea点击:

VBA to ask user if he wants to save the workbook?

excelvbaexcel-vba

提问by user1283776

I want users to get the option to save their workbook (if they have made any changes) at the beginning of my macro. How can I achieve this?

我希望用户可以选择在我的宏的开头保存他们的工作簿(如果他们进行了任何更改)。我怎样才能做到这一点?

I've tried:

我试过了:

ThisWorkbook.Save

but unfortunately it doesn't generate any prompt.

但不幸的是它不会产生任何提示。

I've also tried:

我也试过:

ThisWorkbook.Save(ThisWorkbook.FullName)

but it asks users if they are sure that they want to overwrite the existing file - so it gives a different prompt than the one I want.

但它会询问用户是否确定要覆盖现有文件 - 因此它给出的提示与我想要的提示不同。

回答by David Zemens

Just wrap it with your own prompt:

只需用您自己的提示包装它:

If Not ThisWorkbook.Saved Then 
    If MsgBox("Do you want to save the file before continuing?",vbYesNo,"Save?") = vbYes Then
        ThisWorkbook.Save
    End If
End If