I'm using following code to auto close my workbook after some time its idle. But the code does close the workbook even if the user is active. Any help to sort this would be kindly appreciated.
Option Explicit
Public RunWhen As Double
Public Const cRunWhat = "CloseWorkbook"
Public Sub StartTimer()
RunWhen = Now + TimeValue("00:20:00")
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=True
End Sub
Public Sub CloseWorkbook()
If Workbooks.Count > 1 Then
'More than 1 workbook is open so close only this workbook
ThisWorkbook.Close SaveChanges:=False
Else
'Only 1 workbook is open so close the Excel app, which also closes this workbook
Application.Quit
End If
End Sub
Public Sub StopTimer()
On Error Resume Next
Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=False
End Sub
I have forms in my workbook where users will most of the time use forms to enter data to sheet and Microsoft Access tables. Will the code work for them too?
You could use the MouseMove event of the userforms to reset the timer when the mouse is moved over the userform. You'll have to add the code to each of the userforms:
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Call StopTimer
Call StartTimer
End Sub