【问题标题】:Excel VBA Read Only supersedes BeforeSaveExcel VBA 只读取代 BeforeSave
【发布时间】:2019-02-20 21:48:39
【问题描述】:

我的代码包含用于保存文档 (Excel 365) 的业务逻辑,以确保正确的命名约定、文件位置等作为 Sub Workbook_BeforeSave

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)。在按下 SAVE 按钮之前,Workbook_BeforeSave sub 不会启动。即使在子程序运行后,它也不会离开此屏幕。 有什么办法:

  1. 在只读提示符前面...或
  2. 编写一些代码以离开“另存为”屏幕?

MS Read-Only promt(图一) Save As Screen(图二)

提前非常感谢!

【问题讨论】:

    标签: excel vba before-save


    【解决方案1】:

    这不是一个完美的方法,但试试这个。 这将在按下保存按钮后完全取消保存,您可以添加自己的保存代码。

    编辑:刚刚意识到下面的代码不会停止只读警报,但是当您单击“保存”时,它会取消“保存”并关闭“另存为”菜单。但是,当他们使用 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 之前,效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 2013-07-09
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    相关资源
    最近更新 更多