Partial word, string or wildcard search in Outlook

Search buttonOutlook’s indexed search is fast as long as you know the exact beginning of the word or string that you are looking for.

If you want to search for only a part of a word, like the middle or ending, you’ll have to use the much more cumbersome Advanced Find.

For instance, when searching for “write”, Outlook’s indexed search will only find messages containing “write” but Advanced Find will also find “rewrite”.

A similar situation exists when searching for product codes or reference numbers were people could leave out prefixes, spaces or dashes.

Or what about names?
Katerina or Caterina? Kristian or Christian?

With an Advanced Find macro we can make these type of searches quick and easy again.


Advanced Find

Advanced Find buttonBefore setting up the macro let’s see which actions we need to perform if we were to use Advanced Find without a macro.

In this case we’ll look for a part of a word in the Subject or Message Body field.

  1. Open Advanced Find.
    • keyboard shortcut: CTRL+SHIFT+F
    • Simplified Ribbon (Microsoft 365 and Outlook 2021)
      Click in the Search Field-> … menu on the right-> Search Tools-> Advanced Find…
    • Classic Ribbon
      Click in the Search Field-> Search Tools-> Advanced Find…
  2. Select the Advanced tab.
  3. Add the search within the Subject field;
    • Field: Subject
    • Condition: Contains
    • Value: <your search string>
  4. Click on the “Add to List” button.
  5. Add the search within the Message Body field;
    • Field: Message
    • Condition: Contains
    • Value: <your search string>
  6. Click on the “Add to List” button.
  7. Click on the “Find Now” button.

Setting up an Advanced Find with the “contains” condition.
Setting up an Advanced Find with the “contains” condition.

That is a lot of clicking and then you haven’t even specified which folders to look in and whether or not to include subfolders in your search.

Let’s reduce all that to a single click!

Advanced Find Search Macro

Visual Basic buttonThe Advanced Find Search Macro prompts you to enter your search string. The results will be displayed in a custom Search Folder.

That’s it!

Search box of the Partial Word Search Advanced Find Macro.
Quick and clean Advanced Find macro.

The fact that the results are displayed in a Search Folder has the added benefit of the Reading Pane being available to you, just like with Instant Search.

Additionally, you can use the sorting options of the folder itself and even use the regular Search box to search within the search results.

There are a lot of modifications possible as well, but here are the defaults;

  • Search within the Subject and Message Body field.
  • Search within the Inbox and Sent Items folders, as well as all their subfolders.
  • Results are shown in the Search Folder called: Search Macro

Tip!
Your Search Query itself is stored within the Description field of the Search Folder. Right click on the Search Folder and choose Properties… to see it.

Quick Install

Use the following instructions to configure the macro in Outlook;

  1. Download this code-file (partialwordsearch.zip) or copy the code below.
  2. Open the VBA Editor (keyboard shortcut ALT+F11)
  3. Extract the zip-file and import the PartialWordSearch.bas file via File-> Import…
    If you copied the code, paste it into a new module.
  4. Sign your code so you won’t get any security prompts and the macro won’t get disabled.
  5. Add a button for easy access to the macro or press ALT+F8 and select the macro you want to execute.

You can place a "Open in Browser" button to your Quick Access Toolbar for easy access to the macro.
Add a button of the macro to the QAT for quick access to it.

Macro Code

The following code is contained in the zip-file referenced in the Quick Install. You can use the code below for review or manual installation.

'===============================================================================
'Description: Outlook macro to search for partial word with Advanced Find.
'
' author : Robert Sparnaaij
' version: 1.0
' website: https://www.howto-outlook.com/howto/partial-word-search.htm
'===============================================================================

Sub SearchMacro()
    On Error GoTo ErrorHandler
    Dim strFrom As String
    Dim strSearch As String
    Dim strSearchFolderName As String
    
    strSearchFolderName = "Search Macro"
    
    'Delete "Search Macro" Search Folder from a previous search
    Dim oSearchFolders As Outlook.Folders
    Dim oFolder As Outlook.Folder
    Set oSearchFolders = Outlook.Session.DefaultStore.GetSearchFolders
    
    For Each oFolder In oSearchFolders
        If oFolder.Name = strSearchFolderName Then
            oFolder.Delete
        End If
    Next

    'Prompt for Search Query
    strSearch = InputBox("Enter your search query: ", "Search Macro")

    'Search within the Subject and Message Body
    Dim strDASLFilter As String
    strDASLFilter = "urn:schemas:httpmail:subject LIKE '%" & strSearch & "%' OR " & _
                    "urn:schemas:httpmail:textdescription LIKE '%" & strSearch & "%'"

    'Set search scope to the Inbox and Sent Items (will include subfolders)
    Dim strScope As String
    strScope = "'Inbox', 'Sent Items'"

    'Perform the search
    Dim objSearch As Search
    Set objSearch = Application.AdvancedSearch(Scope:=strScope, Filter:=strDASLFilter, SearchSubFolders:=True, Tag:="SearchFolder")

    'Save the search results to a Search Folder
    objSearch.Save (strSearchFolderName)
    
    'Add the Description and display the Search Folder in the current Outlook window
    Set oSearchFolders = Outlook.Session.DefaultStore.GetSearchFolders
    For Each oFolder In oSearchFolders
        If oFolder.Name = strSearchFolderName Then
            oFolder.Description = "You searched for: " & strSearch
            Set Application.ActiveExplorer.CurrentFolder = oFolder
        End If
    Next

    Set objSearch = Nothing
    Set oSearchFolders = Nothing
    Exit Sub

'Error handling
ErrorHandler:
MsgBox "Error: " & Err & vbNewLine & vbNewLine & Error(Err)

End Sub

Modifications

How? buttonAs you may have some specific needs for your search, there are a couple of things that you may want to modify.

Below are a couple of examples, on how to adjust the search macro to your liking.

Modification 1: Add additional fields to search in

The fields to search in are specified here;

strDASLFilter = "urn:schemas:httpmail:subject LIKE '%" & strSearch & "%' OR " & _
                "urn:schemas:httpmail:textdescription LIKE '%" & strSearch & "%'"

You can look up fields on Microsoft Docs or use this method.

To look in all “frequently-used” text fields use;

strDASLFilter = "urn:schemas:httpmail:fromname LIKE '%" & strSearch & "%' OR " & _
                "urn:schemas:httpmail:displayto LIKE '%" & strSearch & "%' OR " & _
                "urn:schemas:httpmail:displaycc LIKE '%" & strSearch & "%' OR " & _
                "urn:schemas:httpmail:subject LIKE '%" & strSearch & "%' OR " & _
                "urn:schemas:httpmail:thread-topic LIKE '%" & strSearch & "%' OR " & _
                "urn:schemas:httpmail:textdescription LIKE '%" & strSearch & "%' OR " & _
                "urn:schemas:httpmail:messageflag LIKE '%" & strSearch & "%' OR " & _
                "http://schemas.microsoft.com/mapi/received_by_name LIKE '%" & strSearch & "%' OR " & _
                "http://schemas.microsoft.com/mapi/proptag/0x0044001f LIKE '%" & strSearch & "%' OR " & _
                "http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8586001f LIKE '%" & strSearch & "%' OR " & _
                "http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/85a4001f LIKE '%" & strSearch & "%' OR " & _
                "http://schemas.microsoft.com/mapi/id/{00062041-0000-0000-C000-000000000046}/8904001f LIKE '%" & strSearch & "%'"

Modification 2: Specify specific folders to search in

The folders to search in are specified here;

strScope = "'Inbox', 'Sent Items'"

To search in specific folders only, you’ll have to specify the exact FolderPath of the folder.

For instance, to search in the Newsletter folder that is a subfolder of your Inbox folder use;

strScope = "'roady@outlook.com\Inbox\Newsletters'"

It is important to enclose the folder name in single quotes and the entire string in double quotes. Individual folders can be separated with a comma.

Modification 3: Search the entire mailbox

To search the entire mailbox, look for

strScope = "'Inbox', 'Sent Items'"

and change it into;

strScope = "'\\'"

As that will also return all non-email results, you can alter the search filter to only look for emails;

strDASLFilter = "(urn:schemas:httpmail:subject LIKE '%" & strSearch & "%' OR " & _
                "urn:schemas:httpmail:textdescription LIKE '%" & strSearch & "%')" & _
		"AND http://schemas.microsoft.com/mapi/proptag/0x001a001e = 'IPM.Note'"

Modification 4: Open Search Results in a new Outlook window

If you’d rather open the search results in a new Outlook window, look for

Set Application.ActiveExplorer.CurrentFolder = oFolder

and change it into;

oFolder.Display

Advanced: Finding namespace properties for DASL Filter

Filter buttonAs mentioned, you can lookup namespace properties to construct the DASL Filter on Microsoft Docs.

Another way to find out how to build the DASL Filter, is to use the Filter function of the View Settings dialog.

  1. Open the View Settings dialog;
    • Simplified Ribbon (Microsoft 365 and Outlook 2021)
      View-> Current View-> View Settings…
    • Classic Ribbon
      View-> View Settings
  2. Click on the “Filter…” button.
  3. Use the Advanced tab to specify your field based filter.
  4. Select the SQL tab to see the DASL Filter.
    You can enable the “Edit…” checkbox to easily copy/paste the property name.

Namespace properties for the DASL Filter can be found via the SQL tab in View Settings.
Oops! Was her name Katerina or Caterina?
Doesn’t matter for Advanced Find!