How to detect when a workbook is closing?

后端 未结 6 2392
后悔当初
后悔当初 2021-02-15 13:12

The Workbook.BeforeClose event triggers when the workbook is about to close but before the saving message prompt which allows cancelling it.

How can I detect when the wo

6条回答
  •  长情又很酷
    2021-02-15 13:58

    This post could be helpful https://www.dummies.com/software/microsoft-office/excel/an-excel-macro-to-save-a-workbook-before-closing/

    I found code below from the book Excel 2016 Power Programming with VBA, by Michael Alexander

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Dim msg As String, ans as integer
        If Me.Saved = False Then
            msg = "Do you want to save?"
            ans = MsgBox(msg, vbquestion+vbyesnocancel)
            Select Case ans
                Case vbYes: Me.Save
                Case vbCancel: Cancel = True
            End Select
        End If
        Call mySub
        Me.Saved = True
    End Sub
    

提交回复
热议问题