首页 > 解决方案 > 等待收到的邮件触发的宏完成,然后再重复下一封收到的邮件

问题描述

我有一个宏,每次收到带有特定主题名称的电子邮件时都会触发。

如果我同时收到多封触发宏的电子邮件,Outlook 将冻结并崩溃,或者宏将仅针对收到的最后一封电子邮件运行。

在那儿

i) Outlook 中的配置在到达收件箱的电子邮件之间设置延迟,创建进入收件箱的电子邮件积压,让宏有时间一直运行?

或者

ii) 一个 VBA 代码,以便宏在当前运行的宏完成后运行?

标签: vbaoutlook

解决方案


下面列出了可能的替代方案:

  • 处理类的NewMailEx事件ApplicationsNewMailEx当新邮件到达收件箱并且在客户端规则处理发生之前触发该事件。您可以使用EntryIDCollection数组中返回的条目 ID 来调用NameSpace.GetItemFromID方法并处理该项目。但是,根据客户端计算机上的设置,新邮件到达收件箱后,垃圾邮件过滤和将新邮件从收件箱移动到另一个文件夹的客户端规则等过程可能会异步发生。

  • 使用计时器定期运行您的自定义 VBA 代码。VBA 没有为此提供任何东西,但您可以使用如下所列的 Windows API 函数:

Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerfunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long

Public TimerID As Long 'Need a timer ID to eventually turn off the timer. If the timer ID <> 0 then the timer is running

Public Sub ActivateTimer(ByVal nMinutes As Long)
  nMinutes = nMinutes * 1000 * 60 'The SetTimer call accepts milliseconds, so convert to minutes
  If TimerID <> 0 Then Call DeactivateTimer 'Check to see if timer is running before call to SetTimer
  TimerID = SetTimer(0, 0, nMinutes, AddressOf TriggerTimer)
  If TimerID = 0 Then
    MsgBox "The timer failed to activate."
  End If
End Sub

Public Sub DeactivateTimer()
Dim lSuccess As Long
  lSuccess = KillTimer(0, TimerID)
  If lSuccess = 0 Then
    MsgBox "The timer failed to deactivate."
  Else
    TimerID = 0
  End If
End Sub

Public Sub TriggerTimer(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idevent As Long, ByVal Systime As Long)
  MsgBox "The TriggerTimer function has been automatically called!"
End Sub

推荐阅读