简体   繁体   中英

Close Workbook if no activity in Excel

Here is the code I have, it theoretically works. The problem I have is that while I am typing or editing a sheet the worksheet saves and shuts down on me.

So it works, just it doesn't reset the timer while I work in the workbook at all.

Option Explicit
Private Sub Workbook_Open()
    EndTime = Now + TimeValue("00:05:00")
    RunTime
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If EndTime Then
        Application.OnTime _
                EarliestTime:=EndTime, _
                Procedure:="CloseWB", _
                Schedule:=False
        EndTime = Empty
    End If
    EndTime = Now + TimeValue("00:10:00")
    RunTime
End Sub

Now this is inside ThisWorkbook, figuring that it should encompass everything. Am I missing something? The timer obviously isn't resetting when it should be.

As far as I can see, the only reasons why the call to cancel the schedule does not work could be:

  • the procedure name is different (CloseWB) or
  • EndTime has changed since the task was initially scheduled or
  • RunTime does something that makes the workbook close...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM