Where to cancel Application.OnTime in VBA - vba

Using VBA in Excel 2003, I'm trying to cancel an Application.OnTime event using the following code:
Application.OnTime EarliestTime:=varNextRunTime, Procedure:="SomeMethod", Schedule:=False
where varNextRunTime is a global variable containing the next time it is due to run. This code runs in the Workbook_BeforeClose event handler so is always run when the workbook is closed, which is my intention.
However, if the user tries to close the workbook, but changes their mind and hits the cancel button when prompted to Save (Yes, No, Cancel), the Application.OnTime event is still cancelled. BeforeClose is always run before they decide to hit cancel, so has anyone got any ideas how I can only cancel the Application.OnTime event when the workbook is closed?

Here's some ideas
http://www.dailydoseofexcel.com/archives/2004/06/16/beforeclose-vs-beforereallyclose/

Check the Saved property of the Workbook in your event handler. If the workbook is unsaved then display your own dialog to find out if the users wants to save changes, not save changes or cancel.
Here's some rough code. Obviously uncomment the line which deals with the Application.OnTime part and change the MsgBox title to something suitable
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim response As Integer
If Not (Me.Saved) Then
response = MsgBox("Do you want to save changes to '" & Me.Name & "'?", vbYesNoCancel, "put title here")
If (response = vbCancel) Then
Cancel = True
ElseIf (response = vbYes) Then
Me.Save
End If
End If
If Not (Cancel) Then
' Application.OnTime EarliestTime:=varNextRunTime, Procedure:="SomeMethod", Schedule:=False
End If
End Sub

Investigate using:
Application.Quit
If you find this command results in the Excel program remaining open although the document has closed, you may want to follow with
ActiveWorkbook.Close False
I'm not in position to test this or give more insights, unfortunately.

A bit late to the show but here is a simple solution that I've come across (and tested):
If a user deactivates the workbook by closing it, the workbook will still remain the ActiveWorkbook when the Workbook_WindowDeactivate event fires. If the user deactivates the workbook by switching to another workbook, then the new workbook will become the ActiveWorkbook by the time Workbook_WindowDeactivate fires. You can use this behavior to determine the action that caused the event to fire:
Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
If Application.ActiveWorkbook.Name = Me.Name Then
'Your code here
End If
End Sub

Related

VBA How to save and close workbook after time of inactivity, unless userform is open?

This is my first time posting on this site, and would appreciate some help. I'm a member of the mrexcel forums, and have received a lot of help from those great people over there, but have not been able to find an answer to my question. At work, I've created a workbook that allows us to enter data into different worksheets via a userform. I'm trying to make this as user-friendly as I possibly can, to attempt to eliminate as many "user-errors" as possible. One issue I'm trying to avoid is a user forgetting to close the sheet when their shift is over, and locking everyone out because the workbook is read-only when another user has the file open. So, I have a code that will save and close the workbook after 40 minutes of inactivity. One issue this is causing (and it is a very minor issue, but one that has been brought up to me by one of my employees), is that every once in a while, coincidentally the user will be entering data into the userform right around that 40 minutes of inactivity, and the workbook will close before they had a chance to submit the data. I was wondering if anyone could help me modify my code so that when it reaches that 40 minutes of inactivity, before it saves and closes, it sees that the userform is open and cancels the action if it is. Here is the code that I've got. Thanks for any help I can get.
In Module 1:
Dim CloseTime As Date
Sub TimeSetting()
CloseTime = Now + TimeValue("00:40:00")
On Error Resume Next
Application.OnTime EarliestTime:=CloseTime, _
Procedure:="SavedAndClose", Schedule:=True
End Sub
Sub TimeStop()
On Error Resume Next
Application.OnTime EarliestTime:=CloseTime, _
Procedure:="SavedAndClose", Schedule:=False
End Sub
Sub SavedAndClose()
ActiveWorkbook.Close Savechanges:=True
End Sub
In ThisWorkbook:
Private Sub Workbook_Open()
Call TimeSetting
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call TimeStop
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Call TimeStop
Call TimeSetting
End Sub
Just call the TimeStop() sub on the Userform_Initialize() event, and then call the TimeSetting() sub when you close the userform
You can see if a userform is loaded by using this function:
Function IsUFLoaded(ByVal UFName As String) As Boolean
Dim UForm As Object
For Each UForm In VBA.UserForms
If UForm.Name = UFName Then
IsUFLoaded = True
Exit For
End If
Next
End Function
And then in SavedAndClose Sub:
Sub SavedAndClose()
' change userform_name to your userform name
If IsUFLoaded("userform_name") = False Then ActiveWorkbook.Close Savechanges:=True
End Sub
Why not, instead of doing what you're suggesting: allowing a user to accidentally leave themselves logged in to a userform which would cause the issue you're trying to avoid, you instead also reset the timer with Call TimeStop and Call TimeSetting within the change events of fields in the userform?

How can I tell which workbook triggered an event?

I have a BeforeClose event in a workbook, however if the user closes the workbook when it is not the active workbook, e.g. from the taskbar, the script executes on the wrong workbook.
Is there a way to tell which workbook triggered the event and reference that workbook rather than ActiveWorkbook?
Private Sub Workbook_BeforeClose(Cancel As Boolean)
' Replaces default Save message box with custom one
' that includes request stats, warnings, and errors.
If Not ActiveWorkbook.Saved Then
UF_Stats.Show
If Not GlobalVariables.bAllowClose Then Cancel = True
End If
End Sub
Use ThisWorkbook instead of ActiveWorkbook – Vincent G

Prompting a macro on close of workbook, but kill the closing if user press "No"

I am trying to execute a macro on close of a workbook.
The macro works perfectly but the problem is the closing function. I want a user to be prompted to say "Yes" or "No" when closing the workbook. If the user presses "Yes" the workbook should save as xlsm and be closed.
If the user presses "No" the macro should be executed so that the user is sent to sheet "Projektinformation" and the workbook should not be closed.
Here is my code, any thoughts?
Sub Auto_Close()
Dim OutPut As Integer
OutPut = MsgBox("Projektinformation ifylld?", vbYesNo, "Projektinformation.")
If OutPut = 6 Then
'Output = 6(Yes)
ThisWorkbook.SaveAs FileFormat:=xlOpenXMLWorkbookMacroEnabled
Else
'Output = 7(No)
Sheets("Projektinformation").Select
End If
End Sub
From your comment, I'm inferring that your code looks something like this on the Workbook_BeforeClose side:
Private Sub Workbook_BeforeClose(Cancel as Boolean)
Call Auto_Close()
End Sub
The problem is the code does exactly what you asked it to! It runs your Auto_Close subroutine (before the workbook closes) and then proceeds to close the workbook!
In order to achieve what you are trying to achieve, you have to change the Cancel parameter, passed into the Workbook_BeforeClose sub, to True. When Cancel = True the workbook will cancel the close event, otherwise it will continue as usual. I would either pass Cancel into your sub by reference and change the flag depending on what your user clicks or make Auto_Close() a function that returns a boolean, indicating whether or not to continue closing the workbook.
EXAMPLE
Private Sub Workbook_BeforeClose(Cancel as Boolean)
If SomeCondition = True Then
Cancel = True '<-- Workbook will stay open
Else
Cancel = False '<-- Workbook will close as usual
End If
End Sub
You are putting the code in the wrong place. It should be in the Workbook.BeforeClose Event event macro in the ThisWorkbook code sheet.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim OutPut As Integer
OutPut = MsgBox("Projektinformation ifylld?", vbYesNo, "Projektinformation.")
If OutPut = 6 Then
'Output = 6(Yes)
ThisWorkbook.SaveAs FileFormat:=xlOpenXMLWorkbookMacroEnabled
Else
'Output = 7(No)
Cancel = True
Sheets("Projektinformation").Select
End If
End Sub
Note the Cancel = True. This tells Excel to halt the close operation and continue processing instructions.

Stop workbook from running 'cancelled' Timed code

Edit: I've managed to solve the problem below by using Application.EnableEvents = False in Workbook A to stop the Workbook_Open() code from running in Workbook B but am still stumped by the problem if anybody care to explain?
I've got Workbook A opening, editing, saving and closing Workbook B. As Workbook B is used by multiple users, I made it switch to Read-Only mode upon opening and display a pop-up message. However, when running the code from Workbook A, I want to stop the message box from opening. I used Application.OnTime to change Workbook B to Read-Only upon opening and to cancel the pending code upon saving within 10 seconds.
Problem: The code works when I open Workbook B directly and save within 10 seconds but when running a code from Workbook A to open=>edit=>save=>close Workbook B, Workbook B will open by itself even after it is closed.
After doing much research and trial and error by trying to use Public variables and Referencing VBA project, I settled on Application.OnTime using the resource below:
http://www.cpearson.com/excel/OnTime.aspx
Below is a sample of my code.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call StopTimer
End Sub
Private Sub Workbook_Open()
Call StartTimer
End Sub
Public RunWhen As Double
Public Const cRunWhat = "Change_File_Access"
Public Const cRunIntervalSeconds = 15
Public DatabaseWB As Workbook
Sub StartTimer()
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
Schedule:=True
End Sub
Sub StopTimer()
MsgBox RunWhen
On Error Resume Next
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
Schedule:=False
End Sub
Sub Change_File_Access()
If Not Workbooks("WorkbookB.xlsm").ReadOnly Then Workbooks("WorkbookB.xlsm").ChangeFileAccess xlReadOnly
MsgBox "This file is set as Read-Only"
End Sub
Some quick testing shows the timer seems to be running within the context of the Workbook that the macro resides in - not the Workbook that you executed it from. So, you need to do one of 2 things - either quit the Excel application completely (probably not convenient) or reset the timer from the Worksheet you're running the code from.
I.e., in WorkbookA:
Dim book As Workbook
Set book = Workbooks.Open("WorkbookB.xlsm")
book.Save
Application.Run book.Name & "!StopTimer"
book.Close

Excel VBA - Application.OnTime not working and WScript.Shell.PopUp timer not working

I have an automated task that will open Excel and run some VBA code at a specific time every morning. If a user has the excel file open I want to warn them the file will be automatically closed in a few moments via a popup message, then shortly thereafter have Excel automatically save and quit. In case the user is away from their computer, I want the popup message to close automatically after 5 seconds so it doesn't freeze my SaveExit subroutine.
The popup message is appearing, but it will not close automatically.
It is only closing if "OK" is clicked. This is preventing the SaveExit routine from running.
Under Microsoft Excel Objects in This Workbook:
Private Sub Workbook_Open()
ThisWorkbook.Worksheets("Pull Data").Activate
Application.OnTime TimeValue("1:00:00 AM"), "CloseWarn", TimeValue("1:02:00 AM")
Application.OnTime TimeValue("1:03:00 AM"), "SaveExit", TimeValue("1:04:00 AM")
End Sub
In a module called autoclose:
Sub SaveExit()
Application.DisplayAlerts = False
With ThisWorkbook
.Save
Application.Quit
End With
End Sub
Sub CloseWarn()
Select Case CreateObject("WScript.Shell").PopUp("closing soon", 5, "Warning")
Case 1, -1
End Select
End Sub
How do I get a popup warning message to display at a specific time, then close if no user input occurs??
Figured it out! Thanks #Evan for pointing me in the right direction. I don't understand why, but when using the Application.OnTime command under Workbook_Open, things work better if I identify the module where the routine is located. After I figured this out I moved my subs to ThisWorkbook to streamline my code. I created a userform called CloseAlert that informs users that Excel is about to close automatically. The userform closes after 5 seconds, or upon user clicking "OK".
Under Microsoft Excel Objects in ThisWorkbook:
Private Sub Workbook_Open()
ThisWorkbook.Worksheets("Pull Data").Activate
Application.OnTime TimeValue("16:08:00"), "ThisWorkbook.ShowCloseAlert"
Application.OnTime TimeValue("16:09:00"), "ThisWorkbook.SaveExit"
End Sub
Private Sub ShowCloseAlert()
CloseAlert.Show vbModeless
Application.OnTime Now + TimeValue("00:00:05"), "Unloadit"
End Sub
Sub SaveExit()
Application.DisplayAlerts = False
With ThisWorkbook
.Save
Application.Quit
End With
End Sub
In a separate module called unload_CloseAlert:
Public Sub Unloadit()
unload CloseAlert
End Sub