Find Address in CC and Move the Email

This sample finds an address in the list of recipients and then moves the email.

Last modified: 2009/11/23 | Accessed: 46.958  | #79
◀ Previous sample Next sample ▶
Determine the "identity" of your emails. Set with SAM the sender and the folder folder for sent items with the help of rules.

With rules you can only look for your own address in the cc field in order to move the email. With this example you can look for any string in CC. If it's found, the email will be moved into a subfolder of the inbox. In this sample the folder is named 'test'.

tip  How to add macros to Outlook
Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
  Dim Ns As Outlook.NameSpace

  Set Ns = Application.GetNamespace("MAPI")
  Set Items = Ns.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
  If TypeOf Item Is Outlook.MailItem Then
    MoveMessage Item
  End If
End Sub

Private Sub MoveMessage(Item As Outlook.MailItem)
  Dim Ns As Outlook.NameSpace
  Dim Recipients As Outlook.Recipients
  Dim Recip As Outlook.Recipient
  Dim Folder As Outlook.MAPIFolder
  Dim Find As String, FolderName As String

  Find = "info"
  FolderName = "test"

  Set Recipients = Item.Recipients
  For Each Recip In Recipients
    If Recip.Type = olCC Then
      If InStr(1, Recip.Address, Find, vbTextCompare) Then
        Set Ns = Application.GetNamespace("MAPI")
        Set Folder = Ns.GetDefaultFolder(olFolderInbox)
        Set Folder = Folder.Folders(FolderName)
        Item.Move Folder
        Exit For
      End If
    End If
End Sub
ReplyAll ReplyAll
ReplyAll alerts you before unintentionally replying all, or if you are a confidential BCC recipient of the e-mail.
email  Send a message