Application.OnTime running when it shouldn't

佐手、 提交于 2021-02-04 06:35:02

问题


I've written a script which generates a Lotus Notes email to publish data at certain time intervals. This script is Macro2 - Macro5. Macro 2 - Macro 5 are all identical, but I've duplicated the script under different subs to try and diagnose my problem. The problem that I am experiencing is sometimes at for instance 8pm, 4 emails will be generated. One email will be triggered correctly by Macro 5, but the other emails sent at 8pm have been triggered by Macro 2. I know this because I've added an extra line to each Macro to indicate in the email which Macro generated it.

I am using the following to call these subs:

In "This Workbook" I have:

Private Sub Workbook_Open()
Call DailyAM
Call DailyPM
 End Sub

In Module 1:

Sub DailyAM()
 Application.OnTime TimeValue("06:00:00"), "Macro2"
 Application.OnTime TimeValue("10:00:00"), "DailyAM"
End Sub

Sub DailyPM()
 Application.OnTime TimeValue("12:01:00"), "Macro3"
 Application.OnTime TimeValue("16:00:00"), "Macro4"
 Application.OnTime TimeValue("20:00:00"), "Macro5"
 Application.OnTime TimeValue("23:59:00"), "DailyPM"
End Sub

Really confused as to why this is happening. Pretty certain the issue has nothing to do with Macro 2-5, but here it is just in case:

Sub Macro5()
    Windows("Silo report test v2.xlsm").Activate
Application.Calculate
    Dim Maildb As Object 'The mail database
    Dim UserName As String 'The current users notes name
    Dim MailDbName As String 'THe current users notes mail database name
    Dim MailDoc As Object 'The mail document itself
    Dim AttachME As Object 'The attachment richtextfile object
    Dim Session As Object 'The notes session
    Dim EmbedObj As Object 'The embedded object (Attachment)

    Set Session = CreateObject("Notes.NotesSession")
    UserName = Session.UserName
    MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
    Set Maildb = Session.GETDATABASE("", MailDbName)
     If Maildb.IsOpen = True Then
          'Already open for mail
     Else
         Maildb.OPENMAIL
     End If
    Set MailDoc = Maildb.CreateDocument

    MailDoc.Form = "Memo"


'Email address array changed for privacy
vaRecipient = VBA.Array("example@example.com")

    MailDoc.SendTo = vaRecipient

    MailDoc.Subject = Range("B1").Value

 Set workspace = CreateObject("Notes.NotesUIWorkspace")

Dim notesUIDoc As Object
Set notesUIDoc = workspace.EditDocument(True, MailDoc)
Call notesUIDoc.GOTOFIELD("Body")
Call notesUIDoc.FieldClear("Body")
Call notesUIDoc.FieldAppendText("Body", Range("B9").Value & vbCrLf & vbCrLf & Range("b10").Value & Range("I10").Value & Range("D10").Value & vbCrLf & Range("b11").Value & Range("I11").Value & Range("D11").Value & vbCrLf & Range("b12").Value & Range("I12").Value & Range("D12").Value & vbCrLf & vbCrLf & Range("b13").Value & Range("I13").Value & Range("D13").Value & vbCrLf & vbCrLf & Range("b14").Value & Range("C14").Value & Range("D14").Value & vbCrLf & vbCrLf & Range("b15").Value & Range("I15").Value & Range("D15").Value & vbCrLf & Range("F4").Value & vbCrLf)
notesUIDoc.Send
notesUIDoc.Close



    MailDoc.PostedDate = Now() 'Gets the mail to appear in the sent items folder

    Set Maildb = Nothing
    Set MailDoc = Nothing
    Set AttachME = Nothing
    Set Session = Nothing
    Set EmbedObj = Nothing

End Sub

回答1:


It looks like you have scheduled some events using Application.OnTime but I can't see any code to disable these events. If you open the workbook 5 times during the day then these events will be schedule 5 times at the specified time and will trigger concurrently. Perhaps you need to disable the events if the workbook is closed to prevent the events firing?

Here is an example to schedule and cancel a single event:

In a MODULE - create a variable to store the scheduled time:

Public scheduleTime As Date

Somewhere in code schedule some code to run:

Sub ScheduleEvent()

    'Code to Run

    'Reschedule 1 hour from now and rerun this Sub
    scheduleTime = Now + TimeValue("01:00:00")
    Application.OnTime scheduleTime, "ScheduleEvent", , True

End Sub

Ensure event are disabled when closing Excel, in ThisWorkbook object:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    On Error Resume Next
    Application.OnTime scheduleTime, "ScheduleEvent", , False
    On Error GoTo 0

End Sub



回答2:


Thanks everyone for your help. Seems you were correct pathDongle, it appears the ontime events that hadn't been triggered were remaining scheduled after the workbook was closed. I presume they must eventually reset when Excel itself is closed or when the computer is re-booted. I've added the following which appears to have now addressed the issue:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    On Error Resume Next
    Application.OnTime TimeValue("12:01:00"), "Macro3", , False
    Application.OnTime TimeValue("16:00:00"), "Macro4", , False
    Application.OnTime TimeValue("20:00:00"), "Macro5", , False
    Application.OnTime TimeValue("06:00:00"), "Macro6", , False
    On Error GoTo 0
End Sub

I'm thinking I might add similar code to the end of the DailyAM and DailyPM sub routines before they get called again at the end of those subs. I'm thinking that if I open the workbook at 6pm, then 4pm ontime event would otherwise also end up scheduled twice on the following day.

Thanks again.



来源:https://stackoverflow.com/questions/27005522/application-ontime-running-when-it-shouldnt

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