|
'关闭excel时先执行endtimer(判断flag的值),或把endtimer放在workbook的退出事件里执行
'不够稳定,有时会闪退。自己可以试一下。建立一个新工作簿,,,。
Option Explicit
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
Public flag As Boolean
Sub test()
If flag Then
EndTimer
Else
StartTimer
End If
flag = Not flag
End Sub
Sub StartTimer()
TimerSeconds = 1
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)
Debug.Print Timer, flag
End Sub |
|