Option Explicit
Private Const deltaText As String = "09:59:59"
Sub runOnTime()
Dim timeNow As Variant, timeCell As Date, delta As Date
Dim sh As Worksheet
Set sh = ActiveSheet
timeCell = sh.Range("A1").Value
delta = TimeValue(deltaText)
If Not IsDate(timeCell) Then Exit Sub
If timeCell > 1 Then
timeNow = Now()
Else
timeNow = Time()
End If
If timeNow + TimeValue(deltaText) = timeCell Then
saySomething sh.Range("B1")
Else
Debug.Print "time difference is: " & CDate(timeCell - timeNow),
If timeCell < timeNow + delta Then
MsgBox "Time slot missed.", vbOKOnly + vbExclamation
Else
MsgBox "try again in " & CDate(timeCell - delta - timeNow), vbOKOnly + vbInformation
End If
End If
End Sub
Sub saySomething(ByVal something As String)
Application.Speech.Speak something
End Sub
Sub scheduleSpeech()
Dim timeCell As Date, delta As Date, runTime As Date
Dim sh As Worksheet
Set sh = ActiveSheet
timeCell = sh.Range("A1").Value
delta = TimeValue(deltaText)
If Not IsDate(timeCell) Then Exit Sub
If timeCell < 1 Then
runTime = CDate(Date + timeCell - delta)
Else
runTime = CDate(timeCell + delta)
End If
Debug.Print runTime
If runTime < Now Then
MsgBox "Scheduling time has passed.", vbOKOnly + vbInformation
Else
MsgBox "Speech scheduled for " & runTime
Application.OnTime runTime, "'saysomething """ & sh.Range("B1").Value & """'"
End If
End Sub