Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3220360
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:45:09+00:00 2026-05-17T15:45:09+00:00

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

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:45:09+00:00Added an answer on May 17, 2026 at 3:45 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to create federated Subversion servers? As in one server at location
Is it possible to create a trigger that will not be in a transaction?
Is it possible to create images with PHP (as opposed to simply linking to
Is it possible to create a REST web service using ASP.NET 2.0? The articles
Is it possible to create Windows CE 5.0 images (ie: nk.bin) from VS2005/VS2008 without
Is it possible to create Selenium tests using the Firefox plugin that use randomly
It's possible to create digrams in SQL Server 2000 that can be useful to
Is is possible to create COM component and ActiveX controls in .Net (using c#
Is it possible to create LIGHTWEIGHT transactions using TransactionScope() with SQL2000? Or if not,
Is it possible to create Eclipse plugins that auto-discover eachother? I am developing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.