How can I check for or cancel MULTIPLE pending application.ontime events in excel VBA?

后端 未结 4 363
遇见更好的自我
遇见更好的自我 2021-01-17 23:22

I\'m using the Application.Ontime event to pull a time field from a cell, and schedule a subroutine to run at that time. My Application.Ontime event runs on the Workbook_Be

4条回答
  •  不思量自难忘°
    2021-01-17 23:53

    I think I may have a solution that works, based on some of the advice already given.

    In short, we create a global array and each time the user hits save the SendTime is written to the array. This serves to keep track of all our scheduled times.

    When the workbook is closed, we loop through the array and delete all scheduled times.

    I tested this and it seemed to work on Excel 2003. Let me know how you get on.

    Dim scheduleArray() As String //Set as global array to hold times
    
    Private Sub Workbook_BeforeSave
        SendTime = Sheets("Email").Range("B9")
        AddToScheduleArray SendTime
        Application.OnTime SendTime, "SendEmail"
    End Sub
    
    Private Sub Workbook_BeforeClose
        On Error Resume Next
        Dim iArr As Integer, startTime As String
    
        For iArr = 0 To UBound(scheduleArray) - 1 //Loop through array and delete any existing scheduled actions 
            startTime = scheduleArray(iArr)
            Application.OnTime TimeValue(startTime), "SendEmail", , False
        Next iArr
    End Sub
    
    Sub AddToScheduleArray(startTime As String)
        Dim arrLength As Integer
    
        If Len(Join(scheduleArray)) < 1 Then
            arrLength = 0
        Else
            arrLength = UBound(scheduleArray)
        End If
    
        ReDim Preserve scheduleArray(arrLength + 1) //Resize array
        scheduleArray(arrLength) = startTime //Add start time
    End Sub
    

提交回复
热议问题