VBA异常处理问题? [英] VBA exception handling problem?

查看:66
本文介绍了VBA异常处理问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建此脚本以在收到电子邮件时播放wav文件。 关键是只在工作时间播放声音。 如果在工作时间以外收到电子邮件,则不会播放声音。

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

我在OUtlook中有一条规则,当邮件进来时它会使用这个脚本作品!无论如何,有一段时间。 


I have a rule in OUtlook that uses this script when mail comes in and it works! For a while, anyway. 

我不知道问题是什么,但偶尔在这个脚本中发生错误,我从Outlook得到一个对话框说"错误的规则"和"操作失败。" 发生这种情况时,使用此脚本
的Outlook规则将被禁用。

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.

也许我的异常处理不充分。 我不知道。 可能导致此错误的原因是什么?如何正确处理?

Perhaps my exception handling is inadequate.  I don't know.  What could be causing this error and how do I handle it properly?

Outlook 2010 64位,Windows 7

Outlook 2010 64-bit, Windows 7

推荐答案


Bernie Deitrick,Excel MVP 2000-2010 写道:

而不是规则,为什么不使用

Instead of a rule, why not use the

Application_NewMail

Application_NewMail

事件,并使用逻辑来确定是否使用您在sub中的代码播放声音?

event, and use logic to determine whether to play the sound or not with the code you have in your sub?


提供的回复不是可接受的答案。 建议使用不同的方法,但没有提供解释来说明为什么建议的方法更优越,并且在原始方法不适用的情况下也可以使用。 此外,
不会尝试解释原始方法产生的意外行为。


The response provided is not an acceptable answer.  A suggestion is made to use a different approach, but no explanation is provided to demonstrate why the suggested approach is superior and would work where the original approach does not.  Further, no attempt is made to explain the unexpected behavior resulting from the original approach.


这篇关于VBA异常处理问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆