none
Move emails to specified folder after assigning certain category RRS feed

  • Question

  • Hello im new on VBA, and I would like to have a code that would automatically move emails to match folders or subfolders already created once I assigned a category, and be removed from it as soon as I removed the category, for example:

    I have the folders organized this way:

    1.Inbox

    --1.2. What
    ------1.2.1. Invoice
    ------1.2.3. Budget

    --1.3. When
    ------1.3.1 October
    ------1.3.2 September

    --1.4. Where
    ------1.4.1. Lisbon

    --1.5. Who
    ------1.5.1. John
    ------1.5.2. Stewart
    ------1.5.3. William


    If I assigned the category "John" and "Budget", the message is moved to folder 1.2.3. Budgets and 1.5.1. John, and if i set no category the message will return to inbox. Is this possible?!

    Cheers

    Tuesday, May 22, 2018 1:16 PM

All replies

  • Hi CoNN3CT3D,

    For your requirement, I would suggest you split it into many parts, and the first part would be move mail from inbox to subfolders.

    You could try code below:

    Private WithEvents Items As Outlook.Items
    Private WithEvents objItem As Outlook.MailItem
    Private WithEvents objFolder As Outlook.Folder
    Private Sub Application_Startup()
      Dim olApp As Outlook.Application
      Dim objNS As Outlook.NameSpace
      Set olApp = Outlook.Application
      Set objNS = olApp.GetNamespace("MAPI")
      ' default local Inbox
      Set Items = objNS.GetDefaultFolder(olFolderInbox).Items 
    End Sub
    
    Private Sub Items_ItemChange(ByVal Item As Object)
        Dim prompt As String
        Dim olApp As Outlook.Application
        Set olApp = Outlook.Application
        Set objNS = olApp.GetNamespace("MAPI")
        prompt = Item.Categories
        If InStr(prompt, "John") > 0 Then
            Item.Move objNS.GetDefaultFolder(olFolderInbox).Folders("1.2.What").Folders("1.2.3.Budgets")
        End If
        
    End Sub
    

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 23, 2018 7:17 AM