我创建了这个脚本来在收到电子邮件时播放 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 规则将被禁用。
我的异常处理不充分吗?什么可能导致此错误以及如何正确处理它?</p>
更新:
规则非常基本。除了执行脚本之外,它几乎没有什么作用:
Apply this rule after the message arrives
on this computer only
run Project.PlayASoundDuringBusinessHours
最佳答案
不是对问题的直接回答,但我的解决方案是切换到 ItemAdd。
示例:
http://msdn.microsoft.com/en-us/library/office/aa171270(v=office.11).aspx http://www.outlookcode.com/article.aspx?id=62
关于VBA异常处理失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14532483/