简体   繁体   English

VBA 异常处理失败?

[英]VBA exception handling fail?

I have created this script to play a wav file when I receive an email.我创建了这个脚本来在收到电子邮件时播放 wav 文件。 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!我在 Outlook 中有一个规则,当邮件进来时使用这个脚本并且它工作正常! 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."我不知道问题是什么,但是这个脚本中偶尔会出现错误,我从 Outlook 收到一个对话框,显示“规则错误”和“操作失败”。 When this happens, the Outlook rule that uses this script becomes disabled.发生这种情况时,使用此脚本的 Outlook 规则将被禁用。

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

Not a direct response to the question but my solution was to switch to ItemAdd.不是对问题的直接回答,但我的解决方案是切换到 ItemAdd。

Examples:例子:

http://msdn.microsoft.com/en-us/library/office/aa171270(v=office.11).aspx http://www.outlookcode.com/article.aspx?id=62 http://msdn.microsoft.com/en-us/library/office/aa171270(v=office.11​​).aspx http://www.outlookcode.com/article.aspx?id=62

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM