Create new folder for new sender name and move message into new folder
- by Dave Jarvis
Background
I'd like to have Outlook 2010 automatically move e-mails into folders designated by the person's name. For example:
Click Rules
Click Manage Rules & Alerts
Click New Rule
Select "Move messages from someone to a folder"
Click Next
The following dialog is shown:
Problem
The next part usually looks as follows:
Click people or public group
Select the desired person
Click specified
Select the desired folder
Question
How would you automate those problematic manual tasks? Here's the logic for the new rule I'd like to create:
Receive a new message.
Extract the name of the sender.
If it does not exist, create a new folder under Inbox
Move the new message into the folder assigned to that person's name
I think this will require a VBA macro.
Related Links
http://www.experts-exchange.com/Software/Office_Productivity/Groupware/Outlook/A_420-Extending-Outlook-Rules-via-Scripting.html
http://msdn.microsoft.com/en-us/library/office/ee814735.aspx
http://msdn.microsoft.com/en-us/library/office/ee814736.aspx
http://stackoverflow.com/questions/11263483/how-do-i-trigger-a-macro-to-run-after-a-new-mail-is-received-in-outlook
http://en.kioskea.net/faq/6174-outlook-a-macro-to-create-folders
http://blogs.iis.net/robert_mcmurray/archive/2010/02/25/outlook-macros-part-1-moving-emails-into-personal-folders.aspx
Update #1
The code might resemble something like:
Public WithEvents myOlApp As Outlook.Application
Sub Initialize_handler()
Set myOlApp = CreateObject("Outlook.Application")
End Sub
Private Sub myOlApp_NewMail()
Dim myInbox As Outlook.MAPIFolder
Dim myItem As Outlook.MailItem
Set myInbox = myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Set mySenderName = myItem.SenderName
On Error GoTo ErrorHandler
Set myDestinationFolder = myInbox.Folders.Add(mySenderName, olFolderInbox)
Set myItems = myInbox.Items
Set myItem = myItems.Find("[SenderName] = " & mySenderName)
myItem.Move myDestinationFolder
ErrorHandler:
Resume Next
End Sub
Update #2
Split the code as follows:
Sent a test message and nothing happened. The instructions for actually triggering a message when a new message arrives are a little light on details (for example, no mention is made regarding ThisOutlookSession and how to use it).
Thank you.