简体   繁体   中英

Conditional Forwarding VBA for Outlook Emails

I've got a program that auto generates a numeric score based on several input factors and within that program I am able to create notifications of the score when it hits certain thresholds to an e-mail but am unable to create any conditional rules for that score and when it sends notifications.

Right now, I want to send the notification every few hours. We'll start with 2, but haven't able to test out the frequency.

I haven't been able to try anything because I'm unsure of what to search, but I wanted to try to send the initial notification to a dummy e-mail, setup a conditional where every SECOND (or whatever nth) email is then forwarded to a group of e-mails. Is it at all possible to do this with VBA and is there any guidance on where/how I can start? I don't have VBA experience but can probably wiggle my way through with some practice.

Yes, it is possible. You can use the Forward method of Outlook items which executes the Forward action for an item and returns the resulting copy as a MailItem object. For example:

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

To run the code every N-minutes or hours/days you can set up a timer. The Outlook object model doesn't provide anything for that, but you can use Windows API functions:

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

See Outlook VBA - Run a code every half an hour for more information.

Finally, to get items that correspond to your conditions you may use the Find / FindNext or Restrict methods of the Items class. Read more about them in the following articles:

The AdvancedSearch method of the Application class can also be helpful.

PS But I'd recommend starting from the Getting started with VBA in Office page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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