Create new folder for new sender name and move message into new folder

Posted by Dave Jarvis on Super User See other posts from Super User or by Dave Jarvis
Published on 2012-10-17T20:00:04Z Indexed on 2012/10/17 23:05 UTC
Read the original article Hit count: 554

Filed under:
|
|
|

Background

I'd like to have Outlook 2010 automatically move e-mails into folders designated by the person's name. For example:

  1. Click Rules
  2. Click Manage Rules & Alerts
  3. Click New Rule
  4. Select "Move messages from someone to a folder"
  5. Click Next

The following dialog is shown:

Rules Wizard

Problem

The next part usually looks as follows:

  1. Click people or public group
  2. Select the desired person
  3. Click specified
  4. 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:

  1. Receive a new message.
  2. Extract the name of the sender.
  3. If it does not exist, create a new folder under Inbox
  4. Move the new message into the folder assigned to that person's name

I think this will require a VBA macro.

Related Links

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:

VBA Editing

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.

© Super User or respective owner

Related posts about outlook-2010

Related posts about folder