9

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 workbook is already closing past the point where it can be cancelled without removing nor replacing the saving message with a custom one?

One workaround I have found online is to use the event together with the Workbook.Deactivate event which looks like this:

Code in the workbook:

Private Sub Workbook_BeforeClose(ByRef Cancel As Boolean)

  closing_event = True
  check_time = VBA.Now + VBA.TimeSerial(Hour:=0, Minute:=0, Second:=1)
  Excel.Application.OnTime EarliestTime:=check_time, Procedure:="disable_closing_event"

End Sub

Private Sub Workbook_Deactivate()

  If closing_event Then
    VBA.MsgBox Prompt:="Closing event."
    Excel.Application.OnTime EarliestTime:=check_time, Procedure:="disable_closing_event", Schedule:=False
  End If

End Sub

Code in a module:

Public closing_event As Boolean
Public check_time As Date

Public Sub disable_closing_event()

  closing_event = False

End Sub

One very specific edge case where it triggers incorrectly is if you click to close the workbook and in less than one second close the saving message (press Esc to do it fast enough) and change to another workbook (Alt + Tab) it triggers the Deactivate event with the closing_event condition variable still set to True because disable_closing_event has still not set it to False (scheduled by Application.OnTime for when one second goes by).

I would like to find a solution that isn't so much of a workaround and that works correctly against that edge case.

Edit:

The accepted answer has the best solution in my opinion out of all the current answers. I have modified it for my needs and preference to the following code in the workbook:

Private WorkbookClosing As Boolean

Private Sub Workbook_BeforeClose(Cancel As Boolean)
  WorkbookClosing = True
End Sub

Private Sub Workbook_Deactivate()
  If WorkbookClosing And ThisWorkbook.Name = ActiveWindow.Caption Then
    Workbook_Closing
  Else
    WorkbookClosing = False
  End If
End Sub

Private Sub Workbook_Closing()
  MsgBox "Workbook_Closing event."
End Sub
11
  • It seems the question IS NOT ... "How can I detect when the workbook is already closing past the point where it can be cancelled without removing nor replacing the saving message with a custom one?" because I have answered that. The question here is NOT CLEAR - what else is required ?
    – Tin Bum
    Nov 11, 2019 at 2:54
  • @KneeColas it is not clear how your post is supposed to answer the question you quote. Press Cancel in close confirmation dialog and deactivate the workbook (selecting other workbook opened in the same Excel instance).
    – BrakNicku
    Nov 11, 2019 at 9:46
  • That being the case, the questioner is simply prohibiting a simple solution by the requirement "without removing nor replacing the saving message with a custom one"
    – Tin Bum
    Nov 11, 2019 at 10:58
  • @KneeColas I tried to explain the question the best I could. If there are any doubts simply ask that I will try to clarify them. I made it explicit in the question (before the answer with that solution was posted) that I'm looking for a solution that doesn't involve removing the original message of Excel prompting to save the workbook. While that does remove the simple solution of having your own or none, if it didn't then I wouldn't have any questions about it since I was already aware of that solution. Nov 11, 2019 at 12:28
  • @user7393973: Can you explain why you need this? What is your use case? What do you want to do before the workbook is closing but after the user can cancel the close? Nov 11, 2019 at 14:25

7 Answers 7

4
+100

This is an evolution of my 1st Answer - it detects the edge case problem by comparing the ActiveWindow.Caption against ThisWorkbook.Name so it can detect that issue and deal with it. It's not the most elegant solution but I believe it works.

All Code in the Workbook most of it in DeActivate

Public ByeBye As String

Private Sub Workbook_BeforeClose(Cancel As Boolean)
   ByeBye = "B4C"
End Sub

Private Sub Workbook_Deactivate()
   If ByeBye = "B4C" Then
      If ActiveWindow.Caption = ThisWorkbook.Name Then
         If ThisWorkbook.Saved Then
            MsgBox "No problem - Closing after Saving"
         Else
            MsgBox "No problem - Closing without Saving"
         End If
      Else
         If ThisWorkbook.Saved Then
            MsgBox "No problem - New Workbook Activation"
         Else
            MsgBox "Oops Try Again You Cannot Activate '" & ActiveWindow.Caption & "' until '" & ThisWorkbook.Name & "' has completed processing & IT HAS NOW COMPLETED", vbOKOnly, "Hiding"
            ThisWorkbook.Activate
         End If
      End If
   Else
      MsgBox "No problem - Just Hiding"
   End If
   ByeBye = "Done"
End Sub

Private Sub Workbook_Open()
   ByeBye = "OPENED"
End Sub

In response to comment about saving I tested this for 7 possible combinations as follows

 1) Closing without Edits - No Saving Involved ... MsgBox Prompted with ... No problem - Closing after Saving       
 2) Not closing - Just Switch Workbook - Whether Edited or Not ... MsgBox Prompted with ... No problem - Just Hiding        
 3) Not closing - Switch Workbook - After Edit & Cancel ... MsgBox Prompted with ... Oops Try Again …       
 4) Closing and saving ... MsgBox Prompted with ... No problem - Closing after Saving       
 5) Closing and Saving after a prior Cancel ... MsgBox Prompted with ... No problem - Closing after Saving      
 6) Closing but Not Saving ... MsgBox Prompted with ... No problem - Closing without Saving         
 7) Closing but not Saving after a prior Cancel ... MsgBox Prompted with ... No problem - Closing without Saving        
4
  • This solution is nice but if I click to save in the saving message it doesn't let me save. Nov 13, 2019 at 8:09
  • I will test it again then. Nov 13, 2019 at 12:25
  • You're right. I think I confused that it had to show the "save as" window to pick where to save but it didn't showed because it was a file with a path already so it just saved where it was previously saved. I didn't found any case where this didn't worked, including trying in a file already saved and a new file/workbook that hasn't been saved yet. I did removed the ThisWorkbook.Activate and the MsgBoxes Oops and Hiding to get the result I wanted. Thank you. Nov 13, 2019 at 12:38
  • This saved my day! +1 Sep 17, 2020 at 4:46
2

I think trying to cancel the close event is the wrong approach for what you are trying to do. A better approach would be to have a function that is only called when the workbook is actually closing.

Thank you for the comments regarding OnTime not being called while the dialog is open as that pointed me in the right direction. What we need to test is the time between the workbook deactivation and the closing of either the workbook itself or the save dialog. Using the Excel.Application.OnTime function to set this close time means this is possible as it can be delayed until the save dialogue has closed.

Once we have this time, a simple comparison to the deactivation time allows us to decide whether to call the exit function or not.

I initially ran into issues with the workbook reopening to run the .OnTime procedure, so an artificial delay needs to be added into the Deactivation function so the workbook hasn't closed until the close time has been set. Using the code from here - Delay Macro to allow events to finish we can accomplish this.

In ThisWorkbook

Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Excel.Application.OnTime EarliestTime:=Now, Procedure:="SetCloseTime"
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If Timer < CloseTime + 0.2 Then Call CloseProcedure
End Sub

Private Sub Workbook_Deactivate()
    Delay (0.3)
    If Timer < CloseTime + 0.4 Then Call CloseProcedure
End Sub

In a module

Option Explicit

Public CloseTime As Single

Function SetCloseTime()
    CloseTime = Timer
End Function

Function Delay(Seconds As Single)
    Dim StopTime As Single: StopTime = Timer + Seconds
    Do While Timer < StopTime
        DoEvents
    Loop
End Function

Function CloseProcedure()
    MsgBox "Excel is closing"
End Function

The .OnTime seems to run within one second cycles which dictates the length of the delay and the time difference test has a little leeway added with an additional 1/10th of a second (which I found necessary). These timings could potentially need slight tweaking but have so far worked for me with the different scenarios when closing the workbook.

5
  • It seems that when clicking the save button it doesn't allow saving. Nov 13, 2019 at 8:11
  • Not sure what the issue is as the vanilla workbook works and saves for me but if there are further macros in the workbook, there could be timing issues; However you can also use the BeforeSave event to fire the procedure assuming the document has been marked to close as well: edited to include this (also the delay can be shortened). If the issue is still there though I would need more detail as it's not obvious what is meant above
    – Tragamor
    Nov 13, 2019 at 10:04
  • It seems that the updated answer does work for every case that I can think of and test. I feel like there are still some rare edge cases but I haven't fully understood the code yet to be able to find them. As of right now this is in my opinion the most correct solution from all the answers so unless a better one is posted or a flaw is found I will accept this answer. Thank you, I upvoted it for now. P.S.: I think the functions that don't return any value could be subs. Nov 13, 2019 at 10:45
  • The solution in Knee Colas' answer seems more elegant with all the code in the workbook's events and without timers. Both answers seem correct and working though. Nov 13, 2019 at 12:50
  • For reference; the 'Timer' in the code is just a more accurate (millisecond) version of 'VBA.Now': excelfunctions.net/vba-timer-function.html stackoverflow.com/questions/1470632/…
    – Tragamor
    Nov 15, 2019 at 15:32
1

In order to get around your edge case, you need to handle the case where the workbook is deactivated within 1 second of closing it, but only when the save prompt was displayed.

To check if less than 1 second has elapsed, use a high resolution timer to store the time in the Workbook_BeforeClose event, and then compare against it in the Workbook_Deactivate event. Assuming that clsTimer is a suitable high res timer, your code should now be:

Private MyTimer As clsTimer
Private StartTime As Currency

Private Sub Workbook_BeforeClose(ByRef Cancel As Boolean)

    closing_event = True
    Set MyTimer = New clsTimer
    StartTime = MyTimer.MicroTimer
    check_time = VBA.Now + VBA.TimeSerial(Hour:=0, Minute:=0, Second:=1)
    Excel.Application.OnTime EarliestTime:=check_time, Procedure:="disable_closing_event"

End Sub

Private Sub Workbook_Deactivate()

    If closing_event Then

        If Not ThisWorkbook.Saved Then
            'The Save prompt must have been displayed, and the user clicked No or Cancel or pressed Escape

            If MyTimer.MicroTimer - StartTime < 1 Then
                'The user must have pressed Escape and Alt-Tabbed
                closing_event = False
            Else
                'Your Windows API calls here
            End If
        Else
            'The workbook was saved before the close event, so the Save prompt was not displayed
            'Your Windows API calls here
        End If
        Excel.Application.OnTime EarliestTime:=check_time, Procedure:="disable_closing_event", Schedule:=False
    End If

    Set MyTimer = Nothing

End Sub

The class module for clsTimer looks like this:

Private Declare PtrSafe Function getFrequency Lib "kernel32" _
Alias "QueryPerformanceFrequency" (cyFrequency As Currency) As Long

Private Declare PtrSafe Function getTickCount Lib "kernel32" _
Alias "QueryPerformanceCounter" (cyTickCount As Currency) As Long


Public Function MicroTimer() As Currency

    ' Returns seconds.

    Dim cyTicks1 As Currency
    Static cyFrequency As Currency

    MicroTimer = 0

    ' Get frequency.
    If cyFrequency = 0 Then getFrequency cyFrequency

    ' Get ticks.
    getTickCount cyTicks1

    ' Seconds
    If cyFrequency Then MicroTimer = cyTicks1 / cyFrequency

End Function
7
  • I'm still trying to get this to work and test it but it does seem promising. Nov 12, 2019 at 16:26
  • 1
    Thanks. I've removed the extra End If now. Nov 12, 2019 at 16:28
  • 1
    I don't think this approach will work (but then none of the others will either). The real issue has always been between choosing 'No' and 'Cancel' within the save dialogue. 'Cancel' will keep the document open whereas 'No' will close the document. In both cases the timing from deactivate will be delayed and .Saved = False so there is nothing to tell them apart. It probably requires hooking the save dialogue and checking the time difference between the dialogue closing and window deactivation.
    – Tragamor
    Nov 12, 2019 at 18:58
  • @Tragamor: If you click 'No' the Workbook_Deactivate event will be fired, but if you click 'Cancel' it won't be, so that is how you tell them apart. The edge case only occurs when you click 'Cancel' and then also Alt-Tab to another workbook. I believe my answer deals with that scenario (both if it happens within a second and after more than a second, through the high-res timer and the application.OnTime calls respectively). Nov 13, 2019 at 9:38
  • 1
    That will be because of the sceduled application.ontime call. Try adding Excel.Application.OnTime EarliestTime:=check_time, Procedure:="disable_closing_event", Schedule:=False at the end of the workbook_deactivate event handler. Nov 13, 2019 at 13:39
0

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
2
  • If you cancel the first time the Me.Saved = True makes it close next time you try to close without asking if you want to save. Oct 31, 2019 at 13:36
  • While this is a good solution, I would like to find one that doesn't replace or remove the saving message. So I started a bounty on the question to help. Nov 6, 2019 at 17:06
0

I think deactivate is the best way to capture this. Beforeclose might occur earlier than Save event if the document was not saved. So Excel might prompt to save before closure. But Deactivate is the final event before closure (after save). So this can be used.

1
  • That had already been understood. The bigger challenge was determining in the Deactivate event if the workbook was indeed closing or not. Nov 13, 2019 at 17:07
0

had a similar problem and tried to run some macro before closing but it is dependad whether user wants to save workbook or not.

My solution was the code below, though there is a problem, that window of excel always stays open.

Public ClosedByProgram As Boolean

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If Not ClosedByProgram Then
        Cancel = True
        Dim Ans As String
        Ans = MsgBox("Want to save your changes to '" & ThisWorkbook.Name & "'?", vbYesNoCancel, "Microsoft Excel")
        If Ans = vbNo Then
            ClosedByProgram = True
            ThisWorkbook.Close
        ElseIf Ans = vbYes Then
            Dim STR As String:      STR = "'" & ThisWorkbook.Name & "'!" & "mod16_Versioning.IsSuitableForSaving"
            Dim isForSaving As Boolean:     isForSaving = Application.Run(STR, SaveAsUI)
            If isForSaving Then
                Dim STRToRun As String
                STRToRun = "'" & ThisWorkbook.Name & "'!" & "mod02_Events.BeforeSave"
                Application.Run STRToRun, SaveAsUI
            
                Dim STRVersions As String:      STRVersions = "'" & ThisWorkbook.Name & "'!" & "mod16_Versioning.MakeVersion"
                Dim blankCheck As Boolean:      blankCheck = Application.Run(STRVersions, SaveAsUI)
                
                ClosedByProgram = True
                ThisWorkbook.Close
            End If
        End If
    Else
        ThisWorkbook.Saved = True
        Application.Quit
    End If
    
End Sub
-1

This seems to work Code in the WorkBook

Public ByeBye As String

Private Sub Workbook_BeforeClose(Cancel As Boolean)
   ByeBye = "BB @ " & Now()
End Sub

Private Sub Workbook_Deactivate()
   If Left(ByeBye, 2) = "BB" Then
      ByeBye="Done"
      MsgBox "Closing"
   Else
      ByeBye="Done"
      MsgBox "DeActivating BUT NOT Closing"
   End If
End Sub

Private Sub Workbook_Open()
   ByeBye = "OP @ " & Now()
End Sub

Just uses a public variable ByeBye

  • You must initialise it in WorkBook.Open
  • You must Set it in WorkBook.BeforeClose

and can test it in WorkBook.DeActivate

In case it is needed for this to work even after a VBA crash - and loss of ByeBye value I'm resetting it in the Workbook_SheetChange and in WorkBook_SheetSelectionChange

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
   ByeBye = "SC @ " & Now()
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
   ByeBye = "SSC @ " & Now()
End Sub

The above addendum is really only needed if you were going to use the string default of "" for the tested value - but I'm using "BB @ " & Now() so this is not really needed

6
  • 1
    In absence of explanation for downvoting it is difficult to know how to respond.I can only presume the question is incomplete and there is info on the 3rd party site that is significant that the questioner has omitted from SO. I do not follow links to unknown 3rd party sites.
    – Tin Bum
    Nov 11, 2019 at 2:02
  • I tested the code and when I attempt to close the workbook (book 1), cancel, and then focus another workbook (book 2), it triggers as if I had closed the workbook (book 1). Nov 11, 2019 at 12:21
  • Yes I agree that does happen - this was just not clear to me from the question as a requirement. stackoverflow.com/help/minimal-reproducible-example
    – Tin Bum
    Nov 11, 2019 at 15:15
  • Sorry if the question didn't came out clear enough that it caused confusion. When I read it I understood what it contains but thats because I already know what I'm asking. If you believe it can be improved you can point out how with a comment or suggest an edit that I or someone else will review and likely approve. As for the reproducible example, I don't think there is one as it's not about code not working but how it should be coded to do what I explained in the question. Nov 12, 2019 at 10:54
  • Given that it goes wrong with my 16 lines of code and with the comment of @BrakNicku above and that eliminates any talk of timers or any off-site info - I think that condenses the problem.
    – Tin Bum
    Nov 12, 2019 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.