Skip to main content

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

Author: Siluvia Last Modified: 2020-11-13

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:

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...


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!
Comments (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
je cherchais cette fonction depuis un moment pour classer mes mails.
Malheureusement ça ne fonctionne pas. J'ai outlook 2019 au boulot peut-être est-ce à cause de ça ? ou alors vu que je ne maitrise pas Virtual Basis ai-je fait une bêtise ?
Si quelqu'un peut m'(aider svp, je touche du doigt le graal mais il me manque de l'aide, svp....
This comment was minimized by the moderator on the site
I want to add more feature in this code. For instance, once email moved to business folder, I need this email to be automatically forward to specific email address. Possible?
This comment was minimized by the moderator on the site
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)
This comment was minimized by the moderator on the site
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?
This comment was minimized by the moderator on the site
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.
There are no comments posted here yet
Load More
Leave your comments
Posting as Guest
Rate this post:
0   Characters
Suggested Locations