[英]Excel workbook gets opened repeatedly after closing
我使用了Application.ontime()方法來調度一些宏,在關閉工作簿后,它會一次又一次地打開。 為解決此問題,我在工作簿上設置了另一個事件-BeforeClosed。 現在它顯示運行時錯誤1004:'Object'_Application的方法'OnTime'失敗。即使從Web上獲得幫助上下文,我也無法理解為什么會發生這種情況。 給出以下代碼。
Private Sub Workbook_Open()
starttime = Now + TimeValue("00:02:00")
Application.OnTime EarliestTime:=starttime, Procedure:="startapp", schedule:=True
rtime = TimeValue("14:30:00")
Application.OnTime EarliestTime:=rtime, Procedure:="sendreminder", Schedule:=True
Application.OnTime EarliestTime:=rtime, Procedure:="sendreminder_out",Schedule:=True
Application.OnTime EarliestTime:=rtime, Procedure:="SendReminderFromProxy",Schedule:=True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
MsgBox "Dear" & " " & Environ("USERNAME") & ", " & "Please do not forget to save before closing."
starttime = Now + TimeValue("00:02:00")
Application.OnTime EarliestTime:=starttime, Procedure:="startapp", Schedule:=False
rtime = TimeValue("14:30:00")
Application.OnTime EarliestTime:=rtime, Procedure:="sendreminder", Schedule:=False
Application.OnTime EarliestTime:=rtime, Procedure:="sendreminder_out", Schedule:=False
Application.OnTime EarliestTime:=rtime, Procedure:="SendReminderFromProxy", Schedule:=False
End Sub
取消OnTime
,您需要使用與創建時相同的參數值(第三個參數除外)。
這意味着取消時您不能經過其他時間。
Dim starttime '<<< store the time values for later use...
Dim rtime
Private Sub Workbook_Open()
starttime = Now + TimeValue("00:02:00")
Application.OnTime EarliestTime:=starttime, Procedure:="startapp", schedule:=True
rtime = TimeValue("14:30:00")
Application.OnTime EarliestTime:=rtime, Procedure:="sendreminder", Schedule:=True
Application.OnTime EarliestTime:=rtime, Procedure:="sendreminder_out",Schedule:=True
Application.OnTime EarliestTime:=rtime, Procedure:="SendReminderFromProxy",Schedule:=True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
MsgBox "Dear" & " " & Environ("USERNAME") & ", " & "Please do not forget to save before closing."
'use the global variable here
On Error Resume Next '<< prevent error if no schedule is set
' or if already triggered
Application.OnTime EarliestTime:=starttime, Procedure:="startapp", Schedule:=False
Application.OnTime EarliestTime:=rtime, Procedure:="sendreminder", Schedule:=False
Application.OnTime EarliestTime:=rtime, Procedure:="sendreminder_out", Schedule:=False
Application.OnTime EarliestTime:=rtime, Procedure:="SendReminderFromProxy", Schedule:=False
On Error Goto 0
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.