如何使用 VBA 禁用保存和另存为

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12264572/
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-08 13:56:45  来源:igfitidea点击:

How to Disable Save and Save As using VBA

vbaexcel-vbaexcel

提问by Hannah

I am writing a macro and I need to disable the save function on the workbook that VBA has copied and pasted all the information into. Is this possible?

我正在编写一个宏,我需要禁用 VBA 已将所有信息复制并粘贴到的工作簿上的保存功能。这可能吗?

采纳答案by Ryan McDonough

You can use the Applicationobject to access the toolbar buttons directly:

您可以使用该Application对象直接访问工具栏按钮:

Private Sub Workbook_Open() 
    Application.CommandBars("Worksheet Menu Bar").Controls("File").Controls("Save As...").Enabled = False 
    Application.CommandBars("Worksheet Menu Bar").Controls("File").Controls("Save").Enabled = False 
End Sub 

回答by Francis Dean

You can use the Workbook_BeforeSave event to achieve this, disabling the CommandBars won't stop your users using a shortcut such as CTRL + S.

您可以使用 Workbook_BeforeSave 事件来实现此目的,禁用 CommandBars 不会阻止您的用户使用 CTRL + S 等快捷方式。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    MsgBox "You can't save this workbook!"
    Cancel = True

End Sub

回答by Adam Kruzics

One more side note; I had difficulties with the above codes because my organization uses HUNGARIAN version of Excel. So in case you use NON ENGLISH EXCEL you must specify the Controls elements in your Excel's local language ... in my case what worked was:

另一边注;我在使用上述代码时遇到了困难,因为我的组织使用匈牙利版本的 Excel。因此,如果您使用非英语 EXCEL,则必须以 Excel 的本地语言指定 Controls 元素……在我的情况下,有效的是:

Application.CommandBars("Worksheet Menu Bar").Controls("Fá&jl").Controls("Menté&s má&ské&nt...").Enabled = False Application.CommandBars("Worksheet Menu Bar").Controls("Fá&jl").Controls("Menté&s").Enabled = False

Application.CommandBars("Worksheet Menu Bar").Controls("Fá&jl").Controls("Menté&s má&ské&nt...").Enabled = False Application.CommandBars("Worksheet Menu Bar").Controls("Fá&jl").Controls("Menté&s").Enabled = False