保存前提示信息框yes/no/cancel。是调用宏,否继续保存,取消退出子

Before save, prompt message box with yes/no/cancel. Yes calls Macro, No continues to save, Cancel exits sub

我已经有一个成功的 "custom save" 宏可以保存为带有日期戳。当有人试图手动保存时,我只想让一个消息框询问 运行 它。我基本上需要 "yes" 到 运行 宏,"no" 才能正常保存,"cancel" 才能退出 sub。

然而,每当我文件>保存,或ctrl+s,它只是保存而不提示。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim answer As VbMsgBoxResult
    answer = MsgBox("Would you rather Save-As copy with date stamp?", vbYesNoCancel + vbQuestion + vbDefaultButton1, "You are overwriting the document!")

    If answer = vbYes Then
        Call filesave
    ElseIf answer = vbNo Then
        ActiveWorkbook.Save
    Else
        Exit Sub
    End If
End Sub

您需要将子过程参数中的 Cancel 设置为 True 才能停止当前的保存操作。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Dim answer As VbMsgBoxResult
    answer = msgbox("Would you rather Save-As copy with date stamp?", vbYesNoCancel + vbQuestion + vbDefaultButton1, "You are overwriting the document!")
    If answer = vbYes Then
        'Cancel the current standard save operation
        Cancel = True
        Call filesave
    ElseIf answer = vbNo Then
        'don't do anything; the standard save operation will proceed
    Else
        'Cancel the current standard save operation
        Cancel = True
    End If

End Sub