How to Auto Move the Emails from Unknown Senders to a Specific Folder with Outlook VBA

Since the emails from unknown senders may be malicious emails, you should keep cautious of them. This article will tell you how to auto move such emails to another folder, blocking them from cluttering up your Inbox.

You must have ever received the emails which come from unknown sources. How do you deal with them as usual? In general, it is not suggested to readily trust in them. Do not download their attachments or open the links embedded in them in that they may contain viruses, causing your Outlook data getting infected. In this case, many users want that Outlook can auto identify the email senders and move the emails from unknown senders to a specific folder instead of the Inbox.

Confronted with this requirement, perhaps you will tend to resort to Outlook rule. However, Outlook rule can only permit you to auto move the emails from specific senders, instead of unknown senders. Therefore, you should seek other methods, such as using Outlook VBA. Here we will introduce you how to use VBA to get it.

Auto Move the Emails from Unknown Senders to a Specific Folder

  1. In the first place, launch your Outlook application.
  2. Then press the “Alt + F11” key buttons to access VBA editor.
  3. Next in the new popup “Microsoft Visual Basic for Applications” window, you should double click on the “ThisOutlookSession” project on the left side.
  4. Subsequently, you ought to copy and paste the following VBA codes into the “ThisOutlookSession” project window.
Public WithEvents objMails As Outlook.Items

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

Private Sub objMails_ItemAdd(ByVal Item As Object)
    Dim strSenderEmailAddress As String
    Dim objContacts As Outlook.Items
    Dim objContact As ContactItem
    Dim i As Long
    Dim strFilter As String
    Dim objTargetFolder As Folder
    If TypeOf Item Is MailItem Then
       strSenderEmailAddress = Item.SenderEmailAddress
    End If
    Set objContacts = Outlook.Application.Session.GetDefaultFolder(olFolderContacts).Items
    'Check if the sender is in your default contact folder
    For i = 1 To 3
        strFilter = "[Email" & i & "Address] = " & strSenderEmailAddress
        Set objContact = objContacts.Find(strFilter)
        If Not (objContact Is Nothing) Then
           Exit For
        End If
    'If not, move the new incoming email to "Unknown" folder
    If objContact Is Nothing Then
       Set objTargetFolder = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Folders("Unknown")
       Item.Move objTargetFolder
    End If
End Sub

VBA Codes - Auto Move the Emails from Unknown Senders to a Specific Folder

  1. After that, you should sign this code.
  • Firstly, create a new certificate via Digital Certificates for VBA Projects.
  • Then assign the new digital certificate to the current macro.Digitally Sign This Code
  1. Finally you can restart Outlook to activate the new macro. From now on, all the emails whose senders haven’t been saved in your Contacts folder will be auto moved to a “Unknown” folder, like the following screenshot:Move the Emails to "Unknown" Folder

Handle Your Outlook PST Properly

As Outlook PST file is susceptible to corruption, all users should take care of their PST files. So it’s prudent to get hold of a potent PST repair tool, like DataNumen Outlook Repair, such that you will be able to dispose of abrupt PST errors or PST file corruptions in time.

Author Introduction:

Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including recover sql and outlook repair software products. For more information visit

Comments are closed.