Excel VBA只读将取代BeforeSave
我的代码包含用于保存文档(Excel 365)的业务逻辑,以确保在保存前将正确的命名约定、文件位置等作为子工作簿Excel VBA只读将取代BeforeSave,excel,vba,before-save,Excel,Vba,Before Save,我的代码包含用于保存文档(Excel 365)的业务逻辑,以确保在保存前将正确的命名约定、文件位置等作为子工作簿 Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Cancel = True ''Cancels the Save from the button push or Ctrl+S Application.EnableEvents = False '' code code code
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Cancel = True ''Cancels the Save from the button push or Ctrl+S
Application.EnableEvents = False
'' code code code
Application.EnableEvents = True
End Sub
问题是,如果文件以只读方式打开(大多数情况下都是这样),Excel将提示“文件为只读”(图a)并转到文件功能区中的“另存为”屏幕(图b)。在按下保存按钮之前,Workbook\u BeforeSave
子项不会启动。即使在sub运行后,它也不会离开此屏幕。
有没有办法:
万分感谢 这不是一个完美的方法,但是试试这个。 这将完全取消保存,即使按下保存按钮,您也可以添加自己的保存代码 编辑:刚刚意识到下面的代码不会停止只读警报,但当您单击“保存”时,它会取消保存并关闭“另存为”菜单。但是,当他们使用Ctrl+S时,发送键{ESC}会触发Ctrl+ESC,从而打开星形菜单
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Disable Read Only Alert
Application.DisplayAlerts = False
'Disable Events
Application.EnableEvents = False
'Cancel Save
Cancel = True
'Do whatever code you need in here
'
'Mark Workbook as Saved, allowing for the file to close without an alert even if not saved
ThisWorkbook.Saved = True
'Send Escape Key to leave Save As Menu
Application.SendKeys "{ESC}", 1
'Enable Events
Application.EnableEvents = True
'Enable Alerts
Application.DisplayAlerts = True
End Sub
谢谢你的快速回复。
Application.SendKeys“{ESC}”,1
对于退出“另存为”屏幕非常有效。它解决了用户在无限大范围内按下SAVE按钮时认为什么都没有发生的UI问题。不幸的是,Application.DisplayAlerts=False
没有在Excel的“只读”警告之前进入。我认为这是一场胜利。再次,非常感谢!对编辑的响应:我没有发现您在{ESC}
中提到的问题是在我的环境中打开开始菜单。它就放在End Sub
的前面,效果很好。