将“取消”设置为“真”后,VBA Workbook_BeforeClose仍会提示“保存”窗口。单击该菜单上的“保存”,然后关闭工作簿

时间:2016-12-20 16:53:24

标签: excel vba excel-vba

编辑:

问题是由我公司使用的自定义安全插件引起的。我无能为力,这是一个非常精选的问题,所以我删除了这个问题,所以我不会混淆将来发现这个问题的人。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

你可能试图阻止before_save,只是通过添加一个模块全局布尔值告诉他“我来自before_close”。 如果此布尔值在before_save中为真,则取消= 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