PDA

View Full Version : auto save and close workbook after 15 minutes of inactivity



leeann
10-15-2012, 06:43 PM
Hi all, I used the following vba macro in workbook (from previous thread), hoping someone can help. the workbook saves and closes while still working in a sheet. I would like the work book to save and close if there is no activity for 15 minutes. we have large workbook used by multiple people, we would like to stop staff leaving the workbook open on their desktop. If working in the workbook the code should not save and close.

In Workbook
Private Sub Workbook_Open()
Dim AckTime As Integer, InfoBox As Object
Set InfoBox = CreateObject("WScript.Shell")
AckTime = 15
Select Case InfoBox.Popup("This Workbook will automatically save and close after " & AckTime & " minutes of inactivity", _
AckTime, "Monthly Accruals Workbook", 0)
Case 1, -1
End Select
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Call Timer
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Call Timer
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call Limpa
End Sub

In Module 1
Public vartimer As Variant

Sub Timer()
Call Limpa
vartimer = Format(Now + TimeSerial(0, AckTime, 0), "hh:mm:ss")
If vartimer = "" Then Exit Sub
Application.OnTime TimeValue(vartimer), "Fecha"
End Sub
Sub Fecha()
With Application
.EnableEvents = False
ActiveWorkbook.Save
.Quit
End With
End Sub
Sub Limpa()
On Error Resume Next
Application.OnTime earliesttime:=vartimer, _
procedure:="Fecha", schedule:=False
On Error GoTo 0
End Sub

thanks