メールを受信したときに wav ファイルを再生するスクリプトを作成しました。営業時間中だけ音を鳴らすのがポイント。営業時間外にメールを受信した場合、音は鳴りません。
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
Outlook には、メールが着信したときにこのスクリプトを使用するルールがあり、機能します。とにかく、しばらくの間。
何が問題なのかわかりませんが、ときどきこのスクリプトでエラーが発生し、Outlook から "Rules in error" と "The operation failed." というダイアログが表示されます。これが発生すると、このスクリプトを使用する Outlook ルールが無効になります。
私の例外処理は不十分ですか? このエラーの原因は何ですか?また、適切に処理するにはどうすればよいですか?
アップデート:
ルールは非常に基本的です。スクリプトを実行する以外にはほとんど何もしません。
Apply this rule after the message arrives
on this computer only
run Project.PlayASoundDuringBusinessHours