闲置后closuresExcel,即使在编辑模式下也是如此

我不知道这是可能的,当我在网上寻找答案时,我没有真正find任何东西。 我有一个macros,在5分钟不活动后closures。 除非用户在编辑单元格时定时器不启动,因此它就像一个魅力一样,因此它不会closures。 有没有一种方法让Excel有一个计时器用户处于编辑模式多长时间,然后将其从中取出。 一旦退出编辑模式,macros将启动5分钟的不活动状态。 任何帮助是极大的赞赏!

这里是工作簿模块

Private Sub Workbook_BeforeClose(Cancel As Boolean) Me.Saved = True End Sub Private Sub Workbook_Open() start_Countdown End Sub Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) stop_Countdown start_Countdown End Sub 

这是标准模块

 Option Explicit Public Close_Time As Date Sub start_Countdown() Close_Time = Now() + TimeValue("00:05:00") Application.OnTime Close_Time, "close_WB" End Sub Sub stop_Countdown() Application.OnTime Close_Time, "close_WB", , False End Sub Sub close_wb() ThisWorkbook.Close True End Sub 

您需要在VBA之外的某种进程监视器/任务终止符来执行此操作,因为VBA在用户处于input模式时被“locking”。

你可以试试这个:

 Public Declare Function SetTimer Lib "user32" ( _ ByVal HWnd As Long, _ ByVal nIDEvent As Long, _ ByVal uElapse As Long, _ ByVal lpTimerFunc As Long) As Long Public Declare Function KillTimer Lib "user32" ( _ ByVal HWnd As Long, _ ByVal nIDEvent As Long) As Long Public TimerID As Long Public TimerSeconds As Single Sub StartTimer() TimerSeconds = 5*60 ' how often to "pop" the timer (5 minutes in the example). TimerID = SetTimer(0&, 0&, TimerSeconds * 1000&, AddressOf TimerProc) End Sub Sub EndTimer() On Error Resume Next KillTimer 0&, TimerID End Sub Sub TimerProc(ByVal HWnd As Long, ByVal uMsg As Long, _ ByVal nIDEvent As Long, ByVal dwTimer As Long) ' call here whatever you want to call End Sub 

这总是工作,即使在编辑模式,不要忘记调用StartTimer启动和EndTimer之前退出…

Interesting Posts