繁体   English   中英

条件转发 VBA Outlook 邮件

[英]Conditional Forwarding VBA for Outlook Emails

我有一个程序可以根据几个输入因素自动生成一个数字分数,在该程序中,当它达到电子邮件的特定阈值时,我能够创建分数通知,但无法为此创建任何条件规则得分以及何时发送通知。

现在,我想每隔几个小时发送一次通知。 我们将从 2 开始,但无法测试频率。

我无法尝试任何事情,因为我不确定要搜索什么,但我想尝试将初始通知发送到虚拟电子邮件,设置一个条件,其中每个 SECOND(或任何第 n 个)email 然后转发到一组电子邮件。 是否可以使用 VBA 执行此操作,是否有任何关于从哪里/如何开始的指导? 我没有 VBA 经验,但可以通过一些练习来摆动我的方式。

是的,有可能。 您可以使用 Outlook 项目的Forward方法,该方法对项目执行Forward操作并将生成的副本作为MailItem object 返回。例如:

Sub RemoveAttachmentBeforeForwarding() 
 Dim myinspector As Outlook.Inspector 
 Dim myItem As Outlook.MailItem 
 Dim myattachments As Outlook.Attachments 
 Set myinspector = Application.ActiveInspector 
 If Not TypeName(myinspector) = "Nothing" Then 
   Set myItem = myinspector.CurrentItem.Forward 
   Set myattachments = myItem.Attachments 
   While myattachments.Count > 0 
    myattachments.Remove 1 
   Wend 
   myItem.Display 
   myItem.Recipients.Add "Eugene Astafiev" 
   myItem.Send 
 Else 
   MsgBox "There is no active inspector." 
 End If 
End Sub

要每 N 分钟或小时/天运行一次代码,您可以设置一个计时器。 Outlook object model 没有为此提供任何东西,但您可以使用 Windows API 函数:

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

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

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


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

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

请参阅Outlook VBA - 每半小时运行一次代码以获取更多信息。

最后,要获得符合您条件的项目,您可以使用Items class 的Find / FindNextRestrict方法。在以下文章中阅读更多关于它们的信息:

Application class 的AdvancedSearch方法也很有用。

PS 但我建议从Office 页面中的 VBA 入门开始

暂无
暂无

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

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