我使用了与this answer类似的方法强制用户使用某个按钮进行保存。 我有以下内容:
Dim MacroSave As Boolean
Sub RealSave_Click()
MacroSave = True
ThisWorkbook.Save
MacroSave = False
End Sub
Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not MacroSave Then
Cancel = True 'Cancels any request to save the file
MsgBox "Workbook not saved."
Else
Ret = MsgBox("Are you sure you want to save?", vbCritical Or vbYesNo, "Save File?")
If Ret = vbNo Then Cancel = True
End If
End Sub
以上代码在一段时间内完美运行,但现在它在Code execution has been interrupted
告诉我MacroSave = False
。如果我在错误弹出窗口中点击Continue
,则文件已保存正常。我看不到任何不正常的事情;它就是这个讨厌的弹出窗口。
在DoEvents
之前添加MacroSave = False
(之后
ThisWorkbook.Save
),但代码在DoEvents
上中断。
将该行移至" 保存捕手",但同样的错误
在按钮事件的End Sub
上出现了。
On Error Resume Next
,但不能解决此问题。
答案 0 :(得分:0)
这对我有用......
常规模块:
Option Explicit
Public MacroSave As Boolean
Sub RealSave_Click()
If MsgBox("Are you sure you want to save?", vbExclamation + vbYesNo, _
"Save File?") <> vbYes Then Exit Sub
On Error GoTo haveError
MacroSave = True
ThisWorkbook.Save
MsgBox "Saved!"
haveError:
MacroSave = False
End Sub
ThisWorkbook模块:
Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not MacroSave Then
Cancel = True
MsgBox "Workbook not saved: please use the 'Save changes'" & _
" button to save this workbook"
End If
End Sub
答案 1 :(得分:0)
其他建议很棒,但......归结为this: