Categorizing New Mail in Another Mailbox


In a comment to my New Mail Notification for an Additional Mailbox Tanveer asked if I could help him with some code to set a category on messages arriving in another mailbox’s inbox that meet a certain condition. Tanveer wants messages coming from the sender “Team@gmail.com” in the “Re-engineering Group” mailbox’s inbox assigned to the “Outgoing Email” category. Unfortunately this cannot by done by a rule as would be the case if the messages were arriving in the default mailbox. Rules only work on the default mailbox, not additional mailboxes or folders. Fortunately this is easily done by making a few modifications to the code from the original post. Here is that modified code.

The code for this comes in three parts.

Notes.

  • If you are using the code from the first post, then you should already have the code from Part 1. You don’t need to add it again.
  • If you are using the code from the first post, then you need to change the name of the class in step #3 of Part 2. You will also need to change “FolderMonitor” on lines 1 and 8 of Step 3 to match the class name. Finally, you will need to change the variable “objFM1” on lines 1, 8, and 10 of Step 3 to something new (e.g. objFM2).

Part 1.

Follow these instructions to add this code to Outlook.

  1. Start Outlook
  2. Click Tools > Macro > Visual Basic Editor
  3. If not already expanded, expand Microsoft Office Outlook Objects
  4. If not already expanded, expand Modules
  5. Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
  6. Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
  7. Edit the code as needed. I included comments wherever something needs to or can change
  8. Click the diskette icon on the toolbar to save the changes
  9. Close the VB Editor
Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    ' Purpose: Opens an Outlook folder from a folder path.'
    ' Written: 4/24/2009'
    ' Author:  BlueDevilFan'
    ' Outlook: All versions'
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        bolBeyondRoot As Boolean
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function

Part 2.

Follow these instructions to add this code to Outlook.

  1. If not already expanded, expand Microsoft Office Outlook Objects
  2. Right-click on Class Modules, select Insert > Class
    Module
  3. In the Properties panel click on Name and enter
    FolderMonitor
  4. Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
  5. Edit the code as needed. I included comments wherever something needs to or can change
  6. Click the diskette icon on the toolbar to save the changes
Private WithEvents olkItems As Outlook.Items

Private Sub Class_Terminate()
    Set olkFolder = Nothing
End Sub

Public Sub FolderToWatch(objFolder As Outlook.Folder)
    Set olkItems = objFolder.Items
End Sub

Private Sub olkItems_ItemAdd(ByVal Item As Object)
    If LCase(Item.SenderEmailAddress) = "team@gmail.com" then 
        Item.Categories = "Outgoing Email"
        Item.Save
    End If
End Sub

Part 3.

Follow these instructions to add this code to Outlook.

  1. If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession
  2. Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
  3. Edit the code as needed. I included comment lines wherever something needs to or can change
  4. Click the diskette icon on the toolbar to save the changes
  5. Close the VB Editor
  6. Click Tools > Trust Center
  7. Click Macro Security
  8. Set Macro Security to “Warnings for all macros”
  9. Click OK
  10. Close Outlook
  11. Start Outlook. Outlook will display a dialog-box warning that ThisOutlookSession contains macros and asking if you want to allow them to run. Say yes.
Dim objFM1 As FolderMonitor

Private Sub Application_Quit()
    Set objFM1 = Nothing
End Sub

Private Sub Application_Startup()
    Set objFM1 = New FolderMonitor
    'Edit the folder path on the next line as needed.'
    objFM1.FolderToWatch OpenOutlookFolder("Mailbox - Re-engineering Group\Inbox")
End Sub

25 comments on “Categorizing New Mail in Another Mailbox

  1. Yes David, thats right..

    I want an additional condition to the existing colour category code. The coinf Folder is within the Re-engineering Group mailbox (centralised mailbox)

    • Tanveer,

      To do what you described we need to change the code in Part 2 to something like this. I modified the code in this one to check for “Coin Work” in the subject. If it finds it, then the message is moved to the “Coin” folder. If it does not find it, then the item is checked to see if the sender is team@gmail.com. If it is, then the message is categorized. If it’s not, then nothing happens.

      Private WithEvents olkItems As Outlook.Items
      
      Private Sub Class_Terminate()
          Set olkFolder = Nothing
      End Sub
      
      Public Sub FolderToWatch(objFolder As Outlook.Folder)
          Set olkItems = objFolder.Items
      End Sub
      
      Private Sub olkItems_ItemAdd(ByVal Item As Object)
          If InStr(1, Item.Subject, "Coin Work") Then
              'On the next line edit the path to the Coin folder in the Re-engineering Group mailbox as needed
              Item.Move OpenOutlookFolder("Re-engineering Group\Coin")
          Else
              If LCase(Item.SenderEmailAddress) = "team@gmail.com" then
                  Item.Categories = "Outgoing Email"
                  Item.Save
              End If
          End If
      End Sub
      
    • Hey, Tanveer.

      I want to make sure I understand the requirement. Do I understand correctly that you want to add an additional condition to the existing code that checks for messages with the phrase “Coin Work” in the subject. When it gets a hit you want the message moved to a folder called “Coin”. If that’s correct, then I’ll modify the code accordingly.

  2. Hi David,

    In addition to the above requirement, i have an additional requirement ie if i receive any emails with “Coin Work” in subject line then all such emails should move to another sub-folder within the same centralized mailbox…how can i go about with that..

    • Hi, Tanveer.

      Didn’t see your comments until just now. Glad you were able to work it out.

  3. I modified the code to add another condition for Autoforward Category in lines 12 – 15 of the code in part 2 however now facing trouble to assign “Purple Category” basis a word in subject line..pls assist

  4. Davis, inadition to the above code, if i wanna add one more category (Purple Category) but that is basis a word in the subject line..so for that do i use item.subject or mailitem.subject?

    BAsically, any emails that contain “Read Receipt” or “Out of Office” in the subject should be colour coded as “Purple Category”

  5. i also want to know if i can have 2 different codes on ThisOutlookSession referring it to a Class Module2 cause as i understand the Moudule 1 code will remain the same, am i right?

    • I’m not sure I understand the question. You can have as much code in ThisOutlookSession as you want. The only limit is that class names have to be unique and there can only be one Application_Startup and/or Application_Quit subroutine.

  6. David, in addition to the above requirement, i want to assign another category on same mailbox so will i have to write the Step 2 & 3 again?

    My requirement in addition to the above one is, in the same mailbox i have a sub folder within my Inbox named “Archive”. I want to assign “Autoforward” category to all the emails that come directly to that folder post which i want to move all the emails from (“Mailbox – ARM Process Re-engineering Group”).Folders(“Inbox”).Folders(“Archive”) to (“Mailbox – ARM Process Re-engineering Group”).Folders(“Inbox”)..

    • No. It’s not necessary to repeat the code to assign another category on the same mailbox so long as the messages are coming to the inbox. You could simply add another condition like that in lines 12 – 15 of the code in part 2. Mail doesn’t come directly to any folder save the inbox. If mail is going to a subfolder, then it’s because a rule or macro is moving it there after arriving in the inbox. That gives you a couple of ways to handle this. First, you could add the necessary conditional statements required to identify those messages that go to the Archive folder and add code to categorize them and move them to the folder (i.e. Archive). Second, you could use a second macro that’s called by a rule.

  7. David, one last question….
    What if i wana add 2 email IDs in the Item.SenderName, that is any emails received from “GTMS Escalations” or from “GTI Escalations” should be categorised under the “Outgoing Email” category..

    • Change line #12 of the second part from

      If LCase(Item.SenderName) = “GTMS Escalations” then

      to

      If (LCase(Item.SenderName) = “GTMS Escalations”) Or (LCase(Item.SenderName) = “GTI Escalations”) then

  8. Oh wow it worked, i chaged one statement in 2nd Part, instead of Item.SenderEmailAddress i put Item.SenderName.
    Thanks Alot David 🙂

  9. I did change the path as suggested, this is the path (“Mailbox – ARM Process Re-engineering Group”).Folders(“Inbox”).
    I just had to change the path in the 3rd part right? Apart from the path i also changed the sender’s email address which is an another group mailbox ID (GTMS.Escalations)

  10. Thanks David.

    I followed the steps given by you however it doesnt seem to be working. The New mail’s category is not changing to “Outgoing Email” directly and strang part is no error is also poping up, am lil confused on why its not working.

    • In the code did you edit the path to the other folder? The most likely cause is that the path is not correct. Can you share that path me?

Leave a reply to Tanveer Cancel reply