我创建了这个脚本来在收到电子邮件时播放 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 中收到一个对话框,显示“规则错误”和“操作失败”。发生这种情况时,使用此脚本的 Outlook 规则将被禁用。
我的异常处理是否不足?什么可能导致此错误,我该如何正确处理?
更新:
规则非常基本。它除了执行脚本之外几乎没有什么作用:
Apply this rule after the message arrives
on this computer only
run Project.PlayASoundDuringBusinessHours