首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在保存之前,使用“是/否/取消”提示消息框。是的呼叫宏,不继续保存,取消退出子

在保存之前,使用“是/否/取消”提示消息框。是的呼叫宏,不继续保存,取消退出子
EN

Stack Overflow用户
提问于 2018-12-17 21:17:54
回答 1查看 2K关注 0票数 0

我已经有一个成功的“自定义保存”宏来保存-就像一个日期戳。我只想有一个消息框,要求运行它时,有人试图手动保存。我基本上需要“是”才能运行宏,“否”才能正常保存,“取消”才能退出sub。

然而,无论何时我file>save或ctrl+s,它只是保存没有提示。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-17 21:29:54

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

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53823119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档