Log in  \/ 
Register  \/ 


How to move emails to specified folder after assigning certain category in Outlook?

Supposing you assigned a category to a received email in your Outlook, after assigning category, you want this email to move automatically to a certain email folder named with the category name. For example, an email assigned with category “Private” will be move to a folder name “Private” in your Outlook. How to achieve it? The method in this article can help you solve the problem with ease.

Move emails to specified folder after assigning certain category with VBA

Move emails to specified folder after assigning certain category with VBA

The below VBA code can help to automatically move emails to specified folder after assigning certain category in Outlook. Please do as follows.

1. Press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window.

2. In the Microsoft Visual Basic for Applications window, double click to open the ThisOutlookSession (Code) window. Then copy below VBA code into the window. See screenshot:

VBA code: Move emails to specified folder after assigning certain category

Private WithEvents xInboxFld As Outlook.Folder
Private WithEvents xInboxItems As Outlook.Items

Private Sub Application_Startup()
    Set xInboxFld = Outlook.Application.Session.GetDefaultFolder(olFolderInbox)
    Set xInboxItems = xInboxFld.Items
End Sub

Private Sub xInboxItems_ItemChange(ByVal Item As Object)
Dim xMailItem As Outlook.MailItem
Dim xFlds As Outlook.Folders
Dim xFld As Outlook.Folder
Dim xTargetFld As Outlook.Folder
Dim xFlag As Boolean
On Error Resume Next
If Item.Class = olMail Then
    Set xMailItem = Item
    xFlag = False
    If xMailItem.Categories <> "" Then
        Set xFlds = Application.Session.GetDefaultFolder(olFolderInbox).Folders
        If xFlds.Count <> 0 Then
            For Each xFld In xFlds
                If xFld.Name = xMailItem.Categories Then
                    xFlag = True
                End If
        End If
        If xFlag = False Then
            Application.Session.GetDefaultFolder(olFolderInbox).Folders.Add xMailItem.Categories, olFolderInbox
        End If
        Set xTargetFld = Application.Session.GetDefaultFolder(olFolderInbox).Folders(xMailItem.Categories)
        xMailItem.Move xTargetFld
    End If
End If
End Sub

3. Save the code and restart the Outlook.

4. From now on, when assigning category to a receiving email, a folder with the category name will be created automatically under the Inbox folder with the email moved in. See screenshot:

Note: All emails assigned with the same category will be moved to the same folder automatically. But if a new category is assigned to an email, a new folder will be created automatically.

Related articles:

The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Remco · 1 months ago
    it works as a charm but i want to take it to the next step. using a different folder instead of the inbox for example a folder called 2020 under the head and then from there the mentioned subfolders based on Categories. i was thinking to add this line.. Set xInboxFld = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).outlook.Application.Session.GetDefaultFolder(xMailItem.Categories)
  • To post as a guest, your comment is unpublished.
    remco · 4 months ago
    worked on all mails not only after read. i created a rule mention a certain name that it gets a catergory. how can make it that it only takes the mails which are read?

  • To post as a guest, your comment is unpublished.
    santos · 10 months ago
    This worked for me. would you be able to edit the code just to have the messages moved to one folder instead of creating one for each category. I have all the emails in one folder regardless of category and sort them in the folder.
  • To post as a guest, your comment is unpublished.
    Alex · 1 years ago
    At first this did not work for me either but after a while it just started to work, I'm not sure why though.
  • To post as a guest, your comment is unpublished.
    elon · 2 years ago
    same here. Didn't work
  • To post as a guest, your comment is unpublished.
    supermankelly · 2 years ago
    Did not work.