Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

excel - Application.OnTime running when it shouldn't

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("[email protected]")

    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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...