How to filter Outlook email using a Rule with a VBA script?

14

3

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?

Grant Bowman

Posted 2015-10-16T19:43:34.727

Reputation: 381

Answers

12

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.

Grant Bowman

Posted 2015-10-16T19:43:34.727

Reputation: 381

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. – Grant Bowman – 2015-10-16T19:50:06.687

1My 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. – Grant Bowman – 2017-04-04T19:53:31.860

2

This option to run a script was missing. Found a related link here: https://www.slipstick.com/outlook/rules/outlook-2016-run-a-script-rules/

– Jayan – 2017-09-17T16:07:50.147

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 – 2019-07-15T23:41:55.797

I haven't tried this code in a while. Getting the mail folders just right took some fiddling for me. For the regular expression the .* means match any one character zero or more times as needed. The . in between means any character. The last .* means all characters afterward. Let me know how it goes. – Grant Bowman – 2019-07-16T05:18:55.303

Thanks for your help. I still can't get it to work - I don't know if it's my regex or my subfolder. Reg1.Pattern = "(.*TESTME.*)" is my regex and I've tried several variations of the subject line: TESTME, 123 TESTME 456, 123TESTME456, etc. All of these should match. – Tensigh – 2019-07-18T21:06:09.800

1Using 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 – 2019-07-18T21:53:03.350