Certain things are not possible using the default e-mail rules like searching subjects or message bodies with wildcards. You can do multiple words but it's not the same if you need the words one character away from each other. How can I write my own Visual Basic for Applications code to filter Outlook email?

1 Answer 1


I looked high and low for all these parts required to make this simple filter happen. Unix procmail filters are so easy to use by comparison. All the Microsoft Outlook wizards get in the way of a simple filter using wildcards. While many email filter conditions Microsoft provides by default are useful nothing can beat the flexibility and customization of running code.

  1. Write your code.

Alt-F11 brings up the VBA code editor. Double click on ThisOutlookSession. Write your code. In my case it's using a regex on the subject line and moving it not to the DefaultFolder but my own pst in a subfolder.

Sub filter(Item As Outlook.MailItem)
    Dim ns As Outlook.NameSpace
    Dim MailDest As Outlook.Folder
    Set ns = Application.GetNamespace("MAPI")
    Set Reg1 = CreateObject("VBScript.RegExp")
    Reg1.Global = True
    Reg1.Pattern = "(.*Abc.20.*)"
    If Reg1.Test(Item.Subject) Then
        Set MailDest = ns.Folders("Personal Folders").Folders("one").Folders("a")
        Item.Move MailDest
    End If
End Sub
  1. Run the code for each incoming email with a Rule.

Under rules select "Manage Rules and Alerts...". The new rule will look like

Apply this rule after the message arrives run Project1.ThisOutlookSession.filter

To get this, for Step 1: Select condition(s): just click next. Confirm it applies to all messages by clicking OK. For Select action(s) check "run a script" then click to choose the filter script and select Next or Finish. For Select exception(s) click Next or Finish. Give it a good name like vba-filter and check Turn on this rule. click Finish. Since it copes to a local folder click OK when it asks to confirm this rule won't work for email you check online or from another device. Click OK to the Rules and Alerts dialog box.

  1. Outlook doesn't like it when macros are not signed. To self-sign your macros create a certificate and use it.
  • I read about a lot of other partial solutions like macros and creating user-defined columns. This allows UI sorting or view filtering but that will edit all the messages as they come in which is not what I wanted to do. Commented Oct 16, 2015 at 19:50
  • 1
    My outlook client would inconsistently disable the rule to run vbscript vba-filter when starting outlook. This caused problems. There is also a character limit on the vbscript to add to each rule. For them to work consistently, though with less flexibility, I rewrote the filters using the standard Outlook filters so they would work all the time. Commented Apr 4, 2017 at 19:53
  • 3
    This option to run a script was missing. Found a related link here: slipstick.com/outlook/rules/outlook-2016-run-a-script-rules
    – Jayan
    Commented Sep 17, 2017 at 16:07
  • The code above doesn't work for me. I put Abc.20 in the subject file but the messages never move out of the Inbox.
    – Tensigh
    Commented Jul 15, 2019 at 23:41
  • 1
    Using the debug window I managed to get the regex to work. Now it's just a matter of moving to the right folders. I think I can figure it out. Thanks for your help.
    – Tensigh
    Commented Jul 18, 2019 at 21:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .