• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

To create a rule with wildcards

Slipstick Systems

› Outlook › Rules, Filters & Views › To create a rule with wildcards

Last reviewed on March 8, 2017     66 Comments

A security update disabled the Run a script option in the rules wizard in Outlook 2010 and all newer Outlook versions. See Run-a-Script Rules Missing in Outlook for more information and the registry key to fix restore it.

Outlook doesn't support wildcards in rules, so you can't use use a * or ? character for wildcards. Instead, just specify the string you want to match. For example, if you want to match anything with "foo" in the subject text, then any the following would cause the rule to fire:

Foobar
Fools
tofoo

Note that the Rules Wizard supports wildcarding for e-mail addresses. By using the "with specific words in the recipient's address" and "with specific words in the sender's address" conditions, you can set rules for everyone from a particular domain, for example. See Creating Rules that Apply to an Entire Domain for more information.

However, these address rules do not work for Exchange Server recipients since the Exchange server addresses do not show up in the message header. To apply a rule to addresses within your Exchange organization (or exclude them), use @ as the word in the address (or exclude messages with @ in the address). See Creating a Rule to Filter Blank Senders for more information.

Display names

While a partial word filter won't work in all circumstances, you can use it to filter the display name of senders. For example, this rule will filter messages from Forum Administrator

rule test

Tip: Cancel the Check names dialog when adding the name to the people or group field:

Create a rule to filter partial display names

Run a Script rule

When a rule that looks for partial words isn't working, you can use a run a script rule. This example shows how to filter for the Sender's display name, but it can be used with any Outlook email field.

Sub CheckSpam(Item As Outlook.MailItem)
  If InStr(LCase(Item.SenderName), "pfizer") Then
    Item.Delete
  End If
End Sub

Using Wildcards in a Script

You can use wildcards in a script, either by using the method below or regex.

So on this topic... how about moving all messages that say
Invoice AI-SO-11786 from My Company
where the invoice number changes each time?

Assuming AI-SO- is in every invoice and only the numbers change, the next example shows one way to use wildcards in a script. It works with 5 or more digits (or characters) following ai-so-. If the letters are always upper case, you could get away with removing LCASE and using upper case letters in the macro.

The rule looks for the word invoice in the subject or body and if found, runs this macro to check the subject.

Sub MoveInvoices(Item As Outlook.MailItem)
Dim MoveFolder As Folder
Set MoveFolder = Session.GetDefaultFolder(olFolderInbox)
Set MoveFolder = MoveFolder.Folders("Move")
     If LCase(Item.Subject) Like LCase("*ai-so-?????*") = True Then
      Item.Move MoveFolder
  End If
End Sub

See Outlook's Rules and Alerts: Run a Script for more information on using a run a script rule.

More Information

Create rules that apply to an entire domain
Rules frequently asked questions
Creating a Rule to Filter Blank Senders (Outlook-tips.net)

To create a rule with wildcards was last modified: March 8th, 2017 by Diane Poremsky

Related Posts:

  • A simple run a rule script marks Outlook messages read when the messag
    Use a run a script rule to mark messages read
  • Creating an AND rule in Outlook Rules
  • Create a rule to delete mail after a number of days
  • Create rules that apply to an entire domain

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Subscribe
Notify of
66 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

Berkeley Goodloe
April 13, 2022 3:04 pm

How would this code be modified to look for any 8-digit number in the subject line?

0
0
Reply
guest_2233445
June 5, 2021 3:18 am

Hi Diane,

I use outlook. I have about 8000 emails in my sent items folder and about the same in my Inbox. If I group them by "From" or "To" there are over 200 groups for each. That is over 400+ in total. For me to create rules and folders for each one will take months of non-stop work. Is there a way that I can automate this process? I am willing to buy 3rd party sofware if necessary in the event that Outlook does not have this capability. Please advise.

Thank you,

SK

0
0
Reply
Diane Poremsky
Author
Reply to  guest_2233445
June 5, 2021 7:46 am

Are you looking to file them in folders? You could use a macro to file by display name or email address - or get the email address and create a folder by display name.
These two show how to get the address - they need tweaked to file by it though.
Sort messages by Sender domain (slipstick.com)
Display the Recipient Email Address in the Sent Items Folder (slipstick.com)

Not a rule-based, but a way to file mail using a macro
Macro to file Outlook email by sender's display name (slipstick.com)

Or... turn on conversation view and leave the sent items in the sent folder - the inbox items can be left or moved to a folder called Archive. Then use Outlook's search features when you need to find mail from someone.

0
0
Reply
Frank
March 26, 2021 8:59 am

Hi Diane, my vba script reports a compile error: Expected Function or Variable. It fails when entering the sub shown on the line below. Any suggestions?

Sub test(Item As Outlook.MailItem)

0
0
Reply
Diane Poremsky
Author
Reply to  Frank
June 5, 2021 7:33 am

Is the message you are testing with an email message? It will fail on meeting invites, bounce messages and other non-email mailed items.

0
0
Reply
Kostas
September 24, 2019 9:26 am

Hi Diane,

Thank you very much for this solution. Managed to make my own script for messages containing numbers that increment.
But, it's not working with messages that have been archived via Enterprise Vault. Any way to overcome the issue ?

Thanks,
Kostas

0
0
Reply
Diane Poremsky
Author
Reply to  Kostas
September 24, 2019 9:47 am

No, I'm not aware of a way around it - its due to how the archiving works. (And can't test theories because i don't use enterprise vault)

1
0
Reply
Kostas
Reply to  Diane Poremsky
September 24, 2019 9:52 am

Thanks for the fast response !
Have a nice day,
Kostas

0
0
Reply
Kostas
Reply to  Kostas
September 25, 2019 7:22 am

Dianne,

It seems I managed somehow to destroy my script. Even a single search isn't working. How can I delete the script and start a new one ?

0
0
Reply
Michael McMahon
July 9, 2019 10:34 pm

Diane -

apologies for dredging up this old post. Can you clarify whether the line:
Set MoveFolder = MoveFolder.Folders("Move")

Is specifying a folder named "Move" as a target or an action?

Here's what I'm trying to do: match inbound emails that have a subject including INC??????? or TASK??????? and move them to a folder called "Pearson" Folder is at the same level as Inbox. Code is as follows:

Public Sub MoveSDMail(Item As Outlook.MailItem)
Dim MoveFolder As Folder
Set Folder = Session.GetDefaultFolder(olFolderInbox).Parent.Folders("Pearson")
Set MoveFolder = MoveFolder.Folders("Move")
If LCase(Item.Subject) Like LCase("*INC???????*") = True Then
Item.Move MoveFolder
ElseIf LCase(Item.Subject) Like LCase("*TASK???????*") = True Then
Item.Move MoveFolder
End If
End Sub

0
0
Reply
Diane Poremsky
Author
Reply to  Michael McMahon
July 9, 2019 11:07 pm

>> Is specifying a folder named "Move" as a target or an action?
Move is the folder name. In that example, the Move is a subfolder of the Inbox. The macro was written like that to keep the number of "dots" down in the folder path (one less dot over your folder path code).

You would use this
Set Folder = Session.GetDefaultFolder(olFolderInbox)
Set MoveFolder = Folder.Parent.Folders("Pearson")

or this
Set MoveFolder = Session.GetDefaultFolder(olFolderInbox).Parent.Folders("Pearson")

or
Set Folder = Session.GetDefaultFolder(olFolderInbox).Parent.Folders("Pearson")
then
Item.Move Folder

0
0
Reply
Brandon Rossman
May 24, 2018 2:08 pm

Hello Diane,
I see you have helped many people with creating macros, and I have a similar request as Steve. I have 20-40 projects going at once, and I always add the work order number in the subject line. It always starts with a W or C and they are 7 digits long. I would like to move these from the inbox and sent box to the project folders that are nested. They are nested like the following example(Inbox/Projects/40-xx/(Wxxxxxxx) Install Sign.

Any help would be greatly appreciated. We are currently using Outlook 2013.

0
0
Reply
Diane Poremsky
Author
Reply to  Brandon Rossman
May 25, 2018 10:44 am

I have some macros that do pretty much what you need - see https://www.slipstick.com/developer/code-samples/vba-file-messages/

0
0
Reply
Brandon Rossman
Reply to  Diane Poremsky
June 5, 2018 4:59 pm

Hello Diane,

I get the an object could not be found error. I changed the name from Clients to Projects. If I change Projects to Inbox, it will run, but does nothing.

Public Sub FindFolder()
Dim Name$
Dim Folders As Outlook.Folders
Dim Folder As Outlook.MAPIFolder

Set m_Folder = Nothing
m_Find = ""

Name = "*" & strCode
If Len(Trim$(Name)) = 0 Then Exit Sub
m_Find = Name

m_Find = LCase$(m_Find)

Set Folder = Session.GetDefaultFolder(olFolderInbox).Parent.Folders("Projects")
LoopFolders Folder.Folders

Thank you,

Brandon Rossman

0
0
Reply
Diane Poremsky
Author
Reply to  Brandon Rossman
June 6, 2018 10:03 am

This: Set Folder = Session.GetDefaultFolder(olFolderInbox).Parent.Folders("Projects")
Tells outlook to look for a folder named Project at the same level as the Inbox and other default outlook folders. Is that were it is?

0
0
Reply
Brandon Rossman
Reply to  Diane Poremsky
June 6, 2018 11:04 am

It's nested under the Inbox folder. When I change that to Inbox it doesn't do anything.

0
0
Reply
Diane Poremsky
Author
Reply to  Brandon Rossman
June 7, 2018 12:28 am

This will get the subfolder of the inbox:
Set Folder = Session.GetDefaultFolder(olFolderInbox).Folders("Projects")

This is the inbox:
Set Folder = Session.GetDefaultFolder(olFolderInbox)

0
0
Reply
Brandon Rossman
Reply to  Diane Poremsky
June 7, 2018 10:43 am

Thank you Diane. Now that it runs, i am getting a different error. When I run the debug it stops at ExtractText = M.SubMatches(0). Below is the code I have pulled from your original post. Sorry I don't know anything about VB Script. I appreciate your help in this.

Function ExtractText(Str As String) ' As String
Dim regEx As New RegExp
Dim NumMatches As MatchCollection
Dim M As Match

'this pattern looks for 7 digits in the subject
With regEx
.Pattern = "W[0-9]{7}"
.IgnoreCase = True
.Global = False
End With

Set NumMatches = regEx.Execute(Str)
If NumMatches.Count = 0 Then
ExtractText = ""
Else
Set M = NumMatches(0)
ExtractText = M.SubMatches(0)
End If
Code = ExtractText
End Function

0
0
Reply
Diane Poremsky
Author
Reply to  Brandon Rossman
July 1, 2018 9:24 am

>> .Pattern = "W[0-9]{7}"
Try changing that to .Pattern = "(W[0-9]{7})"

0
0
Reply
Brandon Rossman
Reply to  Diane Poremsky
June 7, 2018 11:51 am

I was able recreate the error. It says Run-time error '5': Invalid procedure call or argument.

0
0
Reply
Brandon Rossman
Reply to  Diane Poremsky
June 29, 2018 2:13 pm

Hello Diane,

I have made all the changes like you suggested and even moved the Projects folder to the be at the same level as the Inbox. I do not get any error messages, however nothing happens when I log in? Any suggestions? I am using Outlook 2013.

0
0
Reply
Andy Williams
March 15, 2018 2:08 pm

Diane,
I have a site setup to where people can fill out a form and it sends me an email to contact them back. I am receiving a large amount of email from a spammer and I have been able to find something similar in all emails but it is at the last part of the contacts name. I.E. MikeJonesCM, TomJonesCM, FrankJonesCM. The JonesCM is always there in both the Subject and Body of the email. I'm wanting to be able to send these emails to another folder out of my mail email box, so that I can go through them later, just in case a good lead got put in there by mistake. Can the example you used above help with that, or does something else need to be added? Thank you for your time in this.

1
0
Reply
Diane Poremsky
Author
Reply to  Andy Williams
March 15, 2018 6:03 pm

It should work - put jonescm in the field where i used min.

0
0
Reply
Ken
December 5, 2017 8:57 am

Hello Diane,
I have seen your name a lot when looking for answers to outlook questions and now find myself asking for help. I get a lot of email and need to manage this information. Every job we do gets a job# such as 15662-1 or 18569-2. For each job I may get as many as 35 emails for manufacturing kick offs. The subject line always reads (15662-1 Checklist ****). In addition, I create a folder for each job to help organize my inbox. (Inbox/Jobs/15662-1). Would it be possible to sort the inbox and move each email to the appropriate job folder?

0
0
Reply
Diane Poremsky
Author
Reply to  Ken
December 6, 2017 12:12 am

If you can run a VBA script, yes. I have macro that I helped another user write that checks the subject for a 6 digit code and moves the message to a folder of the same name. On his, we used itemadd macros - and watch the inbox and sent folder for new messages. (He wanted sent items in the client folders too.) It could be converted to a run a script rule, but itemadd is more efficient. The code sample is at https://www.slipstick.com/macros/file-messages-keyword.txt

0
0
Reply
shah
Reply to  Diane Poremsky
March 5, 2020 2:28 pm

When I try this code its doesn't recognise the withevents

0
0
Reply
shah
Reply to  Diane Poremsky
March 5, 2020 2:55 pm

I've managed to get past that however its getting a compile error on m.submatches.

Any advise? I'm using outlook 2019

0
0
Reply

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 30 Issue 25

Subscribe to Exchange Messaging Outlook






Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Jetpack plugin with Stats module needs to be enabled.
  • Open Outlook Templates using PowerShell
  • Count and List Folders in Classic Outlook
  • Google Workspace and Outlook with POP Mail
  • Import EML Files into New Outlook
  • Opening PST files in New Outlook
  • New Outlook: Show To, CC, BCC in Replies
  • Insert Word Document into Email using VBA
  • Delete Empty Folders using PowerShell
  • Warn Before Deleting a Contact
  • Classic Outlook is NOT Going Away in 2026
Ajax spinner

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in classic Outlook (Windows).

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Google Workspace and Outlook with POP Mail

Import EML Files into New Outlook

Opening PST files in New Outlook

New Outlook: Show To, CC, BCC in Replies

Insert Word Document into Email using VBA

Delete Empty Folders using PowerShell

Warn Before Deleting a Contact

Classic Outlook is NOT Going Away in 2026

Newest Code Samples

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2025 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

:wpds_smile::wpds_grin::wpds_wink::wpds_mrgreen::wpds_neutral::wpds_twisted::wpds_arrow::wpds_shock::wpds_unamused::wpds_cool::wpds_evil::wpds_oops::wpds_razz::wpds_roll::wpds_cry::wpds_eek::wpds_lol::wpds_mad::wpds_sad::wpds_exclamation::wpds_question::wpds_idea::wpds_hmm::wpds_beg::wpds_whew::wpds_chuckle::wpds_silly::wpds_envy::wpds_shutmouth:
wpDiscuz

Sign up for Exchange Messaging Outlook

Our weekly Outlook & Exchange newsletter (bi-weekly during the summer)






Please note: If you subscribed to Exchange Messaging Outlook before August 2019, please re-subscribe.

Never see this message again.

You are going to send email to

Move Comment