Originally Posted by
GTO
Hi there,
Mark
Well this ALMOST worked...the only problem is that when I automatically load this using the workbook_open event, it wont start counting in the userform =(
from ThisWorkbook module
Private Sub Workbook_Open()
Call startTimeModule
End Sub
Sub resetTimer()
Call Workbook_Open
End Sub
from timerModule [module 1]
Option Explicit
Private lRunHowLong As Long '<--- Store remaining time
Private NextRunTime As Double '<--- Next time to run procedure, stored, so we can cancel OnTime
Private Delay As Long '<--- How long delay in whole seconds
Public Sub startTimeModule()
'Application.OnTime Now + TimeValue("00:00:05"), "timermodule.timeouthappened"
End Sub
Public Sub timeOutHappened()
timeoutForm.Show
End Sub
Public Sub StartIt(PrimeDelay As Long, Runtime As Long)
'// Assign values to our module level variables and start things up //
lRunHowLong = Runtime
Delay = PrimeDelay
PrimeOnTime
End Sub
Public Sub PrimeOnTime()
Dim sArg As String
'// Reset the next time to run //
NextRunTime = Now + TimeSerial(0&, 0&, CLng(Delay))
'// Build the procedure called argument //
sArg = ThisWorkbook.Name & "!" & "'Module1.UpdateLabel'"
Application.OnTime NextRunTime, sArg, , True
End Sub
Public Sub KillOnTime()
Dim sArg As String
sArg = ThisWorkbook.Name & "!" & "'Module1.UpdateLabel'"
'// Resume Next just in case no current timer exists, then kill existing. //
On Error Resume Next
Application.OnTime NextRunTime, sArg, , False
On Error GoTo 0
'// reset stuff //
lRunHowLong = 0
NextRunTime = 0
Delay = 0
End Sub
Private Sub UpdateLabel()
'// Adjust remaining time //
lRunHowLong = lRunHowLong - Delay
'// Since we are in a Standard Module, I think we want to make sure //
'// the Object Module (the Form's module) is still accessible. //
If FormIsLoaded("timeoutForm") Then
timeoutForm.lbTimeDec.Caption = _
"Workbook will automatically Save & Exit in " & lRunHowLong & " seconds..."
If lRunHowLong <= 0 Then
Unload timeoutForm
Application.DisplayAlerts = False
ActiveWorkbook.Save
ActiveWorkbook.Close
Else
PrimeOnTime
End If
End If
End Sub
Function FormIsLoaded(FormName As String) As Boolean
Dim UF As MSForms.UserForm
Dim n As Long
For n = 0 To UserForms.Count - 1
If UserForms(n).Name = FormName Then
FormIsLoaded = True
Exit Function
End If
Next
End Function
in timeoutForm [userform 1]
Option Explicit
Private Sub cbDontSave_Click()
KillOnTime
Unload Me
Application.DisplayAlerts = False
ActiveWorkbook.Close 'dont save, quit
End Sub
Private Sub cbResume_Click()
KillOnTime 'kill the timer for expiration
Unload Me 'unload form
Call ThisWorkbook.open 'restart the large timer
End Sub
Private Sub cbSave_Click()
KillOnTime
Unload Me
Application.DisplayAlerts = False
ActiveWorkbook.Save 'save
ActiveWorkbook.Close 'and quit
End Sub
Private Sub UserForm_Activate()
'change this variables value in order to set how long
'the messagebox is displayed.
Dim expirationTimer As Long
expirationTimer = 30
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
timerModule.StartIt 1, expirationTimer
End Sub