Excel workbook gets opened repeatedly after closing

不打扰是莪最后的温柔 提交于 2021-02-05 12:05:25

问题


I have used the method Application.ontime() for scheduling some macros.After closing the workbook, it gets opened again and again. to overcome this problem, I set another event on workbook- 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. 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

回答1:


When cancelling an OnTime, you need to use the exact same argument values you used when creating it (except for the third argument).

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


来源:https://stackoverflow.com/questions/45458979/excel-workbook-gets-opened-repeatedly-after-closing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!