Kill App macro is restarting app

  • Thread starter Thread starter Spencer Hutton
  • Start date Start date
S

Spencer Hutton

I use this to Save and close the workbook when the user is idle for 10 minutes:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim TimeOut As Date
TimeOut = Now() + TimeValue("00:10:00")
Application.OnTime TimeOut, "KillApp"
End Sub


Sub KillApp()
ThisWorkbook.Save
ThisWorkbook.Close
End Sub

But as soon as the workbook finishes saving and closing, it opens right back
up again??? Can anyone help?
 
Where are you testing to see if the user is idle? It looks like every time
there is a SelectionChange event on this worksheet, you are scheduling
KillApp to occur 10 minutes later. Try this alternate code:

In A VBA code module in the same workbook, paste this code:

Global StartTime As Single
Global Const TimeLimitInMinutes = 10 'idle time threshold
Global Const TimeCheckDelay = "00:01:00" 'how often to check

Sub CheckTime()
Dim NewTime As Single
'Get the time (seconds past midnight) now.
NewTime = Timer
'If StartTime was yesterday, add 86400 seconds to NewTime.
If NewTime < StartTime Then
NewTime = NewTime + 86400
End If
'If TimeLimitInMinutes has expired since StartTime was last
'updated, close the workbook without saving changes.
If (NewTime - StartTime) > (TimeLimitInMinutes * 60) Then
ThisWorkbook.Save
ThisWorkbook.Close SaveChanges:=False
Else
'Otherwise, schedule a call to CheckTime in the future to check
'again later.
Application.OnTime (Now + TimeValue(TimeCheckDelay)), "CheckTime"
End If
End Sub

The code above is set to save & close the workbook if it is
idle for more than 10 minutes. It will check every minute.

Hope this helps,

Hutch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top