不久前,我掀起(或發現)一些代碼,以便在用戶將其打開(例如過夜或全天)後的一段時間後自動關閉共享工作簿。代碼運行良好,除了關閉其駐留的工作簿時;它也關閉所有的工作簿,並且也是優秀的(沒有Application.Quit)。用戶對此感到厭煩,有誰知道我怎麼才能讓它只能關閉(Thisworkbook),而不是所有其他的?Excel - 關閉工作簿的計時器
感謝。下面
代碼:
Option Explicit
' Declarations
Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Private mlngTimerID As Long
' start the timer
Public Sub StartTimer(lngInterval As Long)
mlngTimerID = SetTimer(0, 0, lngInterval, AddressOf TimerCallBack)
End Sub
' when the timer goes off
Public Sub TimerCallBack(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
' stop the timer
StopTimer
' don't save if read only
If ThisWorkbook.ReadOnly = False Then
' save
ThisWorkbook.Save
End If
' exit without saving
ThisWorkbook.Activate
ThisWorkbook.Close False
End Sub
Public Sub StopTimer()
KillTimer 0, mlngTimerID
End Sub
'To use timer:
'To start the timer
'Call startTimer(1000)'1000 = 1 Second
'To stop timer
'Call stopTimer
它完美! – 2011-01-22 13:21:05