代码执行已被保存防止macros中断
我使用了类似的方法来解决这个问题 ,强制用户使用某个button来保存。 我有以下几点:
公共variables
Dim MacroSave As Boolean
button事件
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
Code execution has been interrupted
。 如果我popup错误命中Continue
,文件已保存正常。 我看不到任何工作不正常; 这只是这个讨厌的popup窗口。
我试过了
-
在
MacroSave = False
之前(和ThisWorkbook.Save
之后)添加DoEvents
,但代码在DoEvents
被中断。 -
将该行移动到“ 保存捕获器 ”,但在button事件
End Sub
上出现相同的错误。 -
On Error Resume Next
,这不能消除这个问题。
我做错了什么,如何消除这种情况?
这适用于我…
常规模块:
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
其他build议是伟大的,但是…归结为这 :
- 按下popup窗口中的“debugging”button。
- 按Ctrl + 暂停|中断两次。
- 点击播放button继续。
- 完成后保存文件。