'Excel Idle time-out with a timed message box warning of save and closing in a set time
I have the coding that will enable me to close and save an inactive excel worksheet when not being used for a period of time (15 secs in the example below) however what I would like to do is before the worksheet saves and closes I would like a Warning message box on the screen to indicate that this will save and close in X-minutes Options Save or Continue/cancel?
Enter into "This Workbook"
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call TimeStop
End Sub
Private Sub Workbook_Open()
Call TimeSetting
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Call TimeStop
Call TimeSetting
End Sub
Enter into "module":
Dim CloseTime As Date
Dim WKB As String
Sub TimeSetting()
WKB = ActiveWorkbook.Name
CloseTime = Now + TimeValue("00:00:15")
On Error Resume Next
Application.OnTime EarliestTime:=CloseTime, _
Procedure:="SavedAndClose", Schedule:=True
End Sub
Sub TimeStop()
On Error Resume Next
Application.OnTime EarliestTime:=CloseTime, _
Procedure:="SavedAndClose", Schedule:=False
End Sub
Sub SavedAndClose()
Workbooks(WKB).Close Savechanges:=True
End Sub
Solution 1:[1]
I use messagebox, and it works :) Last sub....
`Sub TimeSetting()
CloseTime = Now + TimeValue("00:01:00")
On Error Resume Next
Application.OnTime EarliestTime:=CloseTime, _
Procedure:="Message", Schedule:=True
End Sub
Sub TimeStop()
On Error Resume Next
Application.OnTime EarliestTime:=CloseTime, _
Procedure:="Message", Schedule:=False
End Sub
Sub Message()
Result = MsgBox("blalbabla", vbYesNo + vbExclamation)
If Result = vbYes Then
MsgBox "Leave me alone :)"
Else
MsgBox "Save and Close"
Workbooks(WKB).Close Savechanges:=True
End If
End Sub`
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ptz |
