taigovinda
Well-known Member
- Joined
- Mar 28, 2007
- Messages
- 2,639
I have a shared file on a LAN. I borrowed some code online to kick out idle users (who presumably forgot to get out of the file). I tweaked it a bit so that it puts them to read-only instead of kicking them out altogether (not sure if it exhibited the undesirable behavior I'm about to describe, before I tweaked it).
Upon opening the workbook, I want the code to wait five minutes and if the user hasn't done anything (selection change, select different sheet) in the workbook then they will be switched to read-only in that workbook. Every time they do something, I want the five minutes to start over again. And of course if they close the workbook, I want the timer to be canceled.
It seems like the 'start over again' and/or 'cancel' bit isn't always working. I'm not sure if it works sometimes or never. If I go into the file, make my changes and close it (save or not)... then if I don't exit my Excel session, the timer will keep going and it will re-open the book to tell me it's switching to read-only.
What did I do wrong? What do I change to ensure the timer is getting reset when they do something and stopped when they close the book?
Thanks!!
Tai
In ThisWorkBook module:
In a different module:
Upon opening the workbook, I want the code to wait five minutes and if the user hasn't done anything (selection change, select different sheet) in the workbook then they will be switched to read-only in that workbook. Every time they do something, I want the five minutes to start over again. And of course if they close the workbook, I want the timer to be canceled.
It seems like the 'start over again' and/or 'cancel' bit isn't always working. I'm not sure if it works sometimes or never. If I go into the file, make my changes and close it (save or not)... then if I don't exit my Excel session, the timer will keep going and it will re-open the book to tell me it's switching to read-only.
What did I do wrong? What do I change to ensure the timer is getting reset when they do something and stopped when they close the book?
Thanks!!
Tai
In ThisWorkBook module:
Code:
Option Explicit
Private Sub Workbook_Open()
Dim myDate As Date
'start a clock... after x minutes of inactivity, shut it without saving so others can get in
'http://excelribbon.tips.net/T008192_Forcing_a_Workbook_to_Close_after_Inactivity.html
Call StopTimer
Call SetTimer
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call StopTimer
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Excel.Range)
Call StopTimer
Call SetTimer
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Call StopTimer
Call SetTimer
End Sub
In a different module:
Code:
Option Explicit
Dim DownTime As Variant, strWkbk As String
Sub SetTimer()
strWkbk = ThisWorkbook.FullName
DownTime = Now + TimeValue("00:05:00")
Application.OnTime EarliestTime:=DownTime, _
Procedure:="ShutDown", Schedule:=True
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime EarliestTime:=DownTime, _
Procedure:="ShutDown", Schedule:=False
End Sub
Sub ShutDown()
'Application.DisplayAlerts = False
If ThisWorkbook.ReadOnly Or IsFileOpen(strWkbk) = False Then 'don't need to kick you out if you're read-only, or if this is running when the file's not open
StopTimer
SetTimer
Exit Sub
End If
'kick you out if you're not read-only
With ThisWorkbook
' .Saved = True
' .Close False 'we won't close it, we'll just flip them to read-only
Application.DisplayAlerts = False
.ChangeFileAccess Mode:=xlReadOnly
Application.DisplayAlerts = True
MsgBox "You were dilly-dallying in the file. Now you've been switched to read-only.", vbInformation, "You're Read-Only Now!"
End With
End Sub