将Cancel设置为true后,VBA Workbook_BeforeClose仍会提示保存窗口。 单击该菜单上的保存,然后closures工作簿

编辑:

这个问题是由我公司使用的自定义安全插件造成的。 我无能为力,这是一个非常挑剔的问题,所以我删除了这个问题,所以我不会把任何人发现这个问题混淆了。

ActiveWorkbook更改为ThisWorkbook

 Private Sub Workbook_BeforeClose(Cancel As Boolean) If ValidateData = True Then Call SendAndSave Else Select Case MsgBox("There are some invalid entries on the worksheet (values can only be between 0 and 5) so the changes were NOT " & _ "sent to the server. Do you still want to close the tool?", vbYesNo, "Warning") Case vbYes ThisWorkbook.Saved = True '/ won't ask the user to save ' but will still close. Case vbNo ThisWorkbook.Saved = True ''/ won't ask the user to save Cancel = True '/ Won't Close End Select End If End Sub 

你可能试着阻止before_save,只需要添加一个模块全局布尔值来告诉他“我来自before_close”。 如果这个boolean在before_save中是真的,那么cancel = true。

 option explicit Private BlockNormalSave as boolean Private Sub Workbook_BeforeSave(Cancel As Boolean) if BlockNormalSave then Cancel=true end sub Private Sub Workbook_BeforeClose(Cancel As Boolean) BlockNormalSave=true application.displayalerts=false 'not sure if prevents save window thisworkbook.saved=true 'thisworkbook.close save:=false 'not sure either end sub