Reputation: 31
I have created this script to play a wav file when I receive an email. The point is to play the sound only during business hours. If the email is received outside business hours, no sound will play.
Private Declare PtrSafe Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As LongPtr, ByVal dwFlags As Long) As Long
Sub PlayWavFile(WavFileName As String, Wait As Boolean)
If Dir(WavFileName) = "" Then Exit Sub ' no file to play
If Wait Then ' play sound synchronously
PlaySound WavFileName, 0, 0
Else ' play sound asynchronously
PlaySound WavFileName, 0, 1
End If
End Sub
Sub PlayASoundDuringBusinessHours(Item As Outlook.MailItem)
Dim SecondsSinceMidnight
Dim SecondsPerHour
Dim NineOclockAm
Dim NineOclockPm
Dim TooEarly
Dim TooLate
On Error GoTo ErrHandler:
SecondsSinceMidnight = Timer
SecondsPerHour = 60 * 60
NineOclockAm = SecondsPerHour * 9
NineOclockPm = SecondsPerHour * 21
TooEarly = Timer < NineOclockAm
TooLate = Timer > NineOclockPm
If Not (TooEarly) And Not (TooLate) Then
PlayWavFile "c:\windows\media\blahblahblah.wav", False
End If
ExitProcedure:
Exit Sub
ErrHandler:
MsgBox Err.Description, _
vbExclamation + vbOKCancel, _
"Error: " & CStr(Err.Number)
Resume ExitProcedure:
End Sub
I have a rule in Outlook that uses this script when mail comes in and it works! For a while, anyway.
I do not know what the problem is, but once in a while an error occurs in this script and I get a dialog from Outlook that says "Rules in error" and "The operation failed." When this happens, the Outlook rule that uses this script becomes disabled.
Is my exception handling inadequate? What could be causing this error and how do I handle it properly?
Update:
The rule is very basic. It does little beyond executing the script:
Apply this rule after the message arrives
on this computer only
run Project.PlayASoundDuringBusinessHours
Upvotes: 2
Views: 1223
Reputation: 9179
Not a direct response to the question but my solution was to switch to ItemAdd.
Examples:
http://msdn.microsoft.com/en-us/library/office/aa171270(v=office.11).aspx http://www.outlookcode.com/article.aspx?id=62
Upvotes: 2