2017-08-02 233 views
0

我已經使用方法Application.ontime()來調度一些宏。關閉工作簿後,它會一次又一次地打開。爲了克服這個問題,我在工作簿上設置了另一個事件 - BeforeClosed。現在它顯示運行時錯誤1004:'對象'應用程序的'OnTime'方法失敗。我沒有得到爲什麼這種情況發生後,甚至從Web重新獲得幫助上下文。下面的代碼給出。Excel工作簿在關閉後重復打開

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 

回答

0

當取消一個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 
+0

我試過使用上面的代碼。它仍然顯示錯誤。 – Pooja

+0

哪一行給出錯誤? –

+0

from workbook_BeforeClose()line- Application.OnTime EarliestTime:= starttime,Procedure:=「startapp」,Schedule:= False – Pooja

相關問題