You can handle the NewMailEx event of the Application class in Outlook. The event is fired when a new message arrives in the Inbox and before client rule processing occurs. It fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem
, MeetingItem
, or SharingItem
. The EntryIDsCollection
string contains the Entry ID that corresponds to that item. Call the NameSpace.GetItemFromID method and process the item.
In the NewMailEx event handler you may check all your conditions and forward the incoming email if required. The MailItem.Forward method executes the Forward action for an item and returns the resulting copy as a MailItem
object. The following VBA sample code shows how to forward mail items:
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