Workbook BeforeClose Cancel button

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,797
Office Version
  1. 2016
Platform
  1. Windows
Hi dear members,

Let's assume there is a workbook project where an API timer (SetTimer) is started in the workbook open event... This timer is supposed to be running thoughout the life of the workbook until it is closed.

In the workbook before_close event, the timer is cancelled by calling the (KillTimer) API. This step in imperative because Excel crashes if the workbook is closed without properly cancelling the API timer.

Now the problem is that if the workbook is unsaved at the time of closing , Excel displays the usual prompt (Save,Don't Save,Cancel) and if the user happens to click on the cancel button, the workbook will remain open but now the timer is already cancelled which I don't want because, as I said, I need the timer to stay running until the workbook is actually really closed.

So maybe what I need is a way of knowing if the user has clicked the Cancel button and if so, restart the timer again.

I was thinking of replacing the beforeclose prompt with a userform containing similar buttons and execute code respectively according to which button was clicked but I find this approach rather messy.

I was wondering if there is a more self-contained way code-wise other than having to use a separate userform... Any ideas ?

Regards.
 
Perhaps this much simpler approach could work for you?
- this forces the user to click on a button to close the file
( for simplicity the user is not given the option to cancel closing)
- allows you to control how the file is exited

1. To prevent user closing with X in the corner, this goes in ThisWorkbook module
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If Not [COLOR=#ff0000]CloseNow[/COLOR] Then
        Cancel = True
        MsgBox "Please close this file with button"
    End If
End Sub

2 To allow user the option to save the file, and for VBA to control what happens at "Close" use a button with this code
Code:
Private Sub CommandButton1_Click()
Application.DisplayAlerts = False

'Close the file with or without saving
If MsgBox("Save?", vbYesNo) <> vbYes Then
    Call KillTimer
    [COLOR=#ff0000]CloseNow[/COLOR] = True
    ThisWorkbook.Close False
Else
    Call KillTimer
    [COLOR=#ff0000]CloseNow[/COLOR] = True
    ThisWorkbook.Close True
End If
Application.DisplayAlerts = True
End Sub

3 Public boolean variable declared in Standard module (placed at top of module above all procedures)
Code:
Public [COLOR=#ff0000]CloseNow[/COLOR] As Boolean
 
Last edited:
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top