Regular Expression Rules in Outlook 2007?

Kevin Coppock picture Kevin Coppock · Oct 5, 2010 · Viewed 49k times · Source

Is it possible to create rules in Outlook 2007 based on a regex string?

I'm trying to add a filter for messages containing a string such as: 4000-10, a four digit number followed by a dash and then a two digit number, which can be anything from 0000-00 to 9999-99.

I was using this as a regex: \b[0-9]{4}\-[0-9]{2}\b but the filter isn't working. I've tried a few other modifications as well with no luck. I wasn't able to find anything concrete online about whether Outlook even supports entering regexes into a rule, though, so I figured I would ask here in case I'm wasting my time.

EDIT: Thanks to Chris's comment below, I was able to implement this filter via a macro. I thought I would share my code below in case it is able to help anyone else:

Sub JobNumberFilter(Message As Outlook.MailItem)
    Dim MatchesSubject, MatchesBody
    Dim RegEx As New RegExp

    'e.g. 1000-10'
    RegEx.Pattern = "([0-9]{4}-[0-9]{2})"

    'Check for pattern in subject and body'
    If (RegEx.Test(Message.Subject) Or RegEx.Test(Message.Body)) Then
        Set MatchesSubject = RegEx.Execute(Message.Subject)
        Set MatchesBody = RegEx.Execute(Message.Body)
        If Not (MatchesSubject Is Nothing And MatchesBody Is Nothing) Then
            'Assign "Job Number" category'
            Message.Categories = "Job Number"
            Message.Save
        End If
    End If
End Sub

Answer

Chris Judge picture Chris Judge · Oct 5, 2010

I do not know if a regex can be used directly in a rule, but you can have a rule trigger a script and the script can use regexes. I hate Outlook.

First, you have to open the script editor via Tools - Macro - Open Visual Basic Editor (Alt-F11 is the shortcut).

The editor will open. It should contain a project outline in a small panel in the top-left corner. The project will be listed as VBAProject.OTM. Expand this item to reveal Microsoft Office Outlook Objects. Expand that to reveal ThisOutlookSession. Double-click ThisOutlookSession to open the code editing pane (which will probably be blank).

Next select Tools menu | References and enable the RegExp references called something like "Microsoft VBScript Regular Expressions 5.5"

You can now create a subroutine to perform your filtering action. Note that a subroutine called by a rule must have a single parameter of type Outlook.MailItem. For example:

' note that Stack Overflow's syntax highlighting doesn't understand VBScript's
' comment character (the single quote) - it treats it as a string delimiter.  To
' make the code appear correctly, each comment must be closed with another single
' quote so that the syntax highlighter will stop coloring everything as a string.'

Public Enum Actions
    ACT_DELIVER = 0
    ACT_DELETE = 1
    ACT_QUARANTINE = 2
End Enum

Sub MyNiftyFilter(Item As Outlook.MailItem)
    Dim Matches, Match
    Dim RegEx As New RegExp
    RegEx.IgnoreCase = True

    ' assume mail is good'
    Dim Message As String: Message = ""
    Dim Action As Actions: Action = ACT_DELIVER

    ' SPAM TEST: Illegal word in subject'
    RegEx.Pattern = "(v\|agra|erection|penis|boner|pharmacy|painkiller|vicodin|valium|adderol|sex med|pills|pilules|viagra|cialis|levitra|rolex|diploma)"
    If Action = ACT_DELIVER Then
        If RegEx.Test(Item.Subject) Then
            Action = ACT_QUARANTINE
            Set Matches = RegEx.Execute(Item.Subject)
            Message = "SPAM: Subject contains restricted word(s): " & JoinMatches(Matches, ",")
        End If
    End If

    ' other tests'

    Select Case Action
        Case Actions.ACT_QUARANTINE
            Dim ns As Outlook.NameSpace
            Set ns = Application.GetNamespace("MAPI")

            Dim junk As Outlook.Folder
            Set junk = ns.GetDefaultFolder(olFolderJunk)

            Item.Subject = "SPAM: " & Item.Subject
            If Item.BodyFormat = olFormatHTML Then
                Item.HTMLBody = "<h2>" & Message & "</h2>" & Item.HTMLBody
            Else
                Item.Body = Message & vbCrLf & vbCrLf & Item.Body
            End If

            Item.Save
            Item.Move junk

        Case Actions.ACT_DELETE
            ' similar to above, but grab Deleted Items folder as destination of move'

        Case Actions.ACT_DELIVER
            ' do nothing'
    End Select
End Sub


Private Function JoinMatches(Matches, Delimeter)
    Dim RVal: RVal = ""

    For Each Match In Matches
        If Len(RVal) <> 0 Then
            RVal = RVal & ", " & Match.Value
        Else
            RVal = RVal & Match.Value
        End If
    Next

    JoinMatches = RVal
End Function

Next, you have to create a rule (Tools - Rules and Alerts) to trigger this script. Click the New Rule button on the dialog to launch the wizard. Select a template for the rule. Choose the "Check messages when they arrive" template from the "Start from a blank rule" category. Click Next.

Choose the "On this machine only" condition (intuitive isn't it?) and click next.

Choose the "run a script" option. At the bottom of the wizard where it shows your new rule, it should read:

Apply this rule after the message arrives
on this machine only
run a script

The phrase "a script" is a clickable link. Click it and Outlook will display a dialog that should list the subroutine you created earlier. Select your subroutine and click the OK button.

You can click Next to add exceptions to the rule or click Finish if you have no exceptions.

Now, as though that process was not convoluted enough, this rule will deactivate every time you stop and restart Outlook unless you sign the script with a code signing key.

If you don't already have a code signing key, you can create one with OpenSSL.

Did I mention that I hate Outlook?