简体   繁体   English

关闭后反复打开Excel工作簿

[英]Excel workbook gets opened repeatedly after closing

I have used the method Application.ontime() for scheduling some macros.After closing the workbook, it gets opened again and again. 我使用了Application.ontime()方法来调度一些宏,在关闭工作簿后,它会一次又一次地打开。 to overcome this problem, I set another event on workbook- BeforeClosed. 为解决此问题,我在工作簿上设置了另一个事件-BeforeClosed。 Now it is showing runtime error 1004:Method 'OnTime' of 'Object'_Application failed.I am not getting why this happening even after reffering the help context from web. 现在它显示运行时错误1004:'Object'_Application的方法'OnTime'失败。即使从Web上获得帮助上下文,我也无法理解为什么会发生这种情况。 Below code is given. 给出以下代码。

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

When cancelling an OnTime , you need to use the exact same argument values you used when creating it (except for the third argument). 取消OnTime ,您需要使用与创建时相同的参数值(第三个参数除外)。

That means you can't pass a different time when you cancel it. 这意味着取消时您不能经过其他时间。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM