簡體   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