Quite a while ago I whipped up (or found) some code to automatically close a shared workbook after a period if the user had left it open (e.g. overnight or all day). The code works well, except for when it closes the workbook within it resides; it also closes all workbooks and excel as well (without an Application.Quit). The users are becoming anoyed at this, does anyone know how I can get it to only close (Thisworkbook), not all the others?
Thanks.
Code below:
Option Explicit
' Declarations
Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Private mlngTimerID As Long
' start the timer
Public Sub StartTimer(lngInterval As Long)
mlngTimerID = SetTimer(0, 0, lngInterval, AddressOf TimerCallBack)
End Sub
' when the timer goes off
Public Sub TimerCallBack(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
' stop the timer
StopTimer
' don't save if read only
If ThisWorkbook.ReadOnly = False Then
' save
ThisWorkbook.Save
End If
' exit without saving
ThisWorkbook.Activate
ThisWorkbook.Close False
End Sub
Public Sub StopTimer()
KillTimer 0, mlngTimerID
End Sub
'To use timer:
'To start the timer
'Call startTimer(1000)'1000 = 1 Second
'To stop timer
'Call stopTimer