繁体   English   中英

关闭后反复打开Excel工作簿

[英]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.

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