在不活动后关闭Excel,即使在编辑模式下



我不确定这是可能的,当我在网上搜索答案时,我没有真正找到任何东西。我有一个宏的地方,它将关闭后5分钟的不活动。工作像一个魅力,除了当用户正在编辑一个单元格计时器不启动,因此它不会关闭,因为这一点。是否有一种方法,excel有一个计时器多长时间的用户是在编辑模式,然后它会把他们出来。一旦退出编辑模式,宏将开始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被"锁定"。

你可以试试:

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开始和结束定时器退出之前…

相关内容

  • 没有找到相关文章

最新更新