Some users hope to classify and archive incoming Outlook emails based on which type of recipient they are – “To”, “CC” or “BCC” recipient. Now, this article will make this come true with Outlook VBA.
Perhaps you’re accustomed to creating various folders and subfolders to separate emails. For instance, you create three subfolders under Inbox. They are “To”, “CC” and “BCC” respectively. Then, you hope, when an incoming email arrives in Inbox, Outlook can auto figure out which type of recipient you are, whether “To”, “CC” or “BCC” recipient, and auto move the mail to corresponding folder. Thereinafter, we will introduce a method to achieve it.
Auto Classify Incoming Emails as Per Which Type of Recipient You Are
- At first, go to Outlook VBA editor by referring to the post – “How to Run VBA Code in Your Outlook“.
- Then, in the new window, put the code below into the “ThisOutlookSession” project.
Private objInboxFolder As Outlook.Folder Private WithEvents objIncomingItems As Outlook.Items Private Sub Application_Startup() Set objInboxFolder = Application.Session.GetDefaultFolder(olFolderInbox) Set objIncomingItems = objInboxFolder.Items End Sub Private Sub objIncomingItems_ItemAdd(ByVal objItem As Object) Dim objMail As Outlook.MailItem Dim objRecipient As Outlook.Recipient Dim strTo, strCC As String Dim objTargetFolder As Outlook.Folder If objItem.Class = olMail Then Set objMail = objItem 'Get the email's recipient lists in different types For Each objRecipient In objMail.Recipients Select Case objRecipient.Type Case olTo strTo = strTo & objRecipient.Address & ";" Case olCC strCC = strCC & objRecipient.Address & ";" End Select Next 'Find which recipient type you are 'Get the target folder If InStr(strTo, "email@example.com") > 0 Then On Error Resume Next Set objTargetFolder = objInboxFolder.Folders("To") If objTargetFolder Is Nothing Then Set objTargetFolder = objInboxFolder.Folders.Add("To") End If ElseIf InStr(strCC, "firstname.lastname@example.org") > 0 Then On Error Resume Next Set objTargetFolder = objInboxFolder.Folders("CC") If objTargetFolder Is Nothing Then Set objTargetFolder = objInboxFolder.Folders.Add("CC") End If Else On Error Resume Next Set objTargetFolder = objInboxFolder.Folders("BCC") If objTargetFolder Is Nothing Then Set objTargetFolder = objInboxFolder.Folders.Add("BCC") End If End If 'Move the email objMail.Move objTargetFolder End If End Sub
Note: You need to alter “email@example.com” to your own real email address.
- Later, move your cursor into the first ‘”Application_Startup” subroutine” and hit “F5” key button.
- From now on, every time when a new email gets into your inbox, Outlook will auto check which recipient field you’re in and move it to relevant folder.
For instance, if you are a CC recipient, the new mail will be automatically moved to the “CC” folder. See as the following figure.
Escape from Outlook Data Loss
No one is ready to experience Outlook file damage. However, in reality, Outlook is prone to crash and corruption. Thus, in order to avoid Outlook data loss, you have to make regular backup for your PST file. Moreover, it is suggested to get hold of a PST repair tool, such as DataNumen Outlook Repair, which is able to recover PST file in a jiffy.
Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including SQL Server recovery and outlook repair software products. For more information visit www.datanumen.com