2017-08-01 365 views
0

我正在開發用於發送文件夾中文檔警報的應用程序。當使用任務調度程序打開工作簿時,它將調用一個宏 - 'startapp'。在startapp()中,它會在每2分鐘後檢查文件夾中的新文件併發送電子郵件/通知。在同一個宏中,有一個對另一個函數的調用,該函數每隔1小時發送文件夾中未決文件的提醒。我已設置遞歸調用startapp(),以便應用程序處於連續處理中。有一個錯誤:在運行時,宏會在每2分鐘後發送新文檔警報以及提醒。我希望應用程序在1小時後發送提醒。請檢查下面的代碼。使用Excel VBA Application.ontime函數和遞歸函數調用

Public Sub startapp() 

Call checkuser(i)      
'finds lastrow and user email for incoming files 

Call checklist(i)    
'check new in files according to user selected in the list and send emails 

rtime = Now + TimeValue("01:00:00") 


Application.OnTime EarliestTime:=rtime, Procedure:="sendreminder", Schedule:="true"   
'sendreminder for files in in folder 

starttime = Now + TimeValue("00:02:00") 


Application.OnTime EarliestTime:=starttime, Procedure:="startapp", Schedule:="true" 


End Sub 
+0

當打開工作簿,通話雙方'startapp'和'sendreminder'。然後刪除有關從'startapp'子例程調度'sendreminder'的代碼,並將其放入'sendreminder'子例程中。 – YowE3K

+0

(我假設提醒不會每隔2分鐘發送一次,直到第一個小時過去。) – YowE3K

+0

我在OnWorkbookopen()中調度了sendreminder(),並將它從startapp()中移除。我保持着另一個。打開工作簿後,它會第一次發送電子郵件,然後在1小時後沒有任何事情發生。我們是否設定回憶這個例程? – Pooja

回答

0

我建議你設置你的代碼,如下所示:

Private Sub Workbook_Open() 
    startapp 
    sendreminder 
End Sub 

Public Sub startapp() 
    checkuser i 
    'finds lastrow and user email for incoming files 

    checklist i 
    'check new in files according to user selected in the list and send emails 

    starttime = Now + TimeValue("00:02:00") 
    Application.OnTime EarliestTime:=starttime, _ 
         Procedure:="startapp", _ 
         Schedule:=True  
End Sub 

Public Sub sendreminder() 
    '... 
    ' whatever code you currently have 
    '... 

    rtime = Now + TimeValue("01:00:00") 
    Application.OnTime EarliestTime:=rtime, _ 
         Procedure:="sendreminder", _ 
         Schedule:=True  
End Sub 
+0

準確地說,我做了同樣的事情,它對我來說最合適。非常感謝YowE3K。 – Pooja