2017-08-01 366 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