我使用了 Application.ontime() 方法来调度一些宏。关闭工作簿后,它会一次又一次地打开。为了解决这个问题,我在工作簿上设置了另一个事件 - BeforeClosed。现在它显示运行时错误 1004:“Object”_Application 的方法“OnTime”失败。即使在从网络引用帮助上下文后,我也不明白为什么会发生这种情况。下面给出了代码。
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)