Hello jeffwest2,
This macro code will close and save the workbook automatically if the mouse hasn't been moved for an hour. Copy this code to a VBA module. You will need to add a macro to the Workbook_Open event and a macro to the Workbook_BeforeClose() event. Samples are shown below.
'Written: October 19, 2009
'Author: Leith Ross
'Summary: Close and Save the workbook if mouse hasn't been moved in 1 hour.
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function GetCursorPos _
Lib "User32.dll" (ByRef lpPoint As POINTAPI) As Long
Private Sub MouseMonitor()
Static Cnt As Long
Dim CurPos As POINTAPI
Static Mins As Long
Static PrevX As Long
Static PrevY As Long
Static Secs As Long
GetCursorPos CurPos
If PrevX <> CurPos.X Or PrevY <> CurPos.Y Then
Secs = 0
Mins = 0
Cnt = 0
PrevX = CurPos.X
PrevY = CurPos.Y
Application.StatusBar = "60 minutes till workbook closes"
End If
Secs = Secs + 1
If Secs >= 3600 Then
ActiveWorkbook.Close True
Exit Sub
End If
Cnt = Cnt + 1
If Cnt >= 60 Then
Mins = Mins + 1
Application.StatusBar = 60 - Mins & " minutes till workbook closes"
Cnt = 0
End If
Application.OnTime Now() + TimeValue("00:00:01"), "MouseMonitor", , True
End Sub
Sub MouseMonitorOn()
Application.StatusBar = ""
Application.DisplayStatusBar = True
Application.OnTime Now() + TimeValue("00:00:01"), "MouseMonitor", , True
End Sub
Sub MouseMonitorOff()
Application.DisplayStatusBar = False
Application.OnTime Now() + TimeValue("00:00:01"), "MouseMonitor", , False
End Sub
Workbook Module Code
Private Sub Workbook_Open()
MouseMonitorOn
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
MouseMonitorOff
End Sub
Bookmarks