Introduction | Example | Tutorial | Applications | Comments

Introduction - VBA Outlook Search

Outlook search not working? Use this VBA code example to search through your email inbox. This tutorial shows you how to regain search functionality if you are one of the thousands of people whose Outlook instant search function has stopped working.

Example - VBA Outlook Search

Create Outlook Search Folders

Public Sub OutlookSearch()
'-------------------------------------------------------------------------------------------------
'---Created by: Ryan Wells------------------------------------------------------------------------
'---Date: 03/2015---------------------------------------------------------------------------------
'---Program: OutlookSearch------------------------------------------------------------------------
'---Purpose: Search Outlook inbox for a particular string and display the results-----------------
'------------in a "Search Folders" folder.--------------------------------------------------------
'-------------------1) Make sure you have an email selected in your inbox.------------------------
'-------------------2) Press "Alt F8"-------------------------------------------------------------
'-------------------3) Select SearchString > Run--------------------------------------------------
'-------------------4) Type your search string----------------------------------------------------
'-------------------5) Click OK-------------------------------------------------------------------
'------------------------Your results will show up in the "Search Folders" folder with------------
'------------------------the title equal to your search string------------------------------------
'-------------------6) You can right-click and delete this search folder when you are finished.---
'-------------------------------------------------------------------------------------------------
Dim strFilter As String
Dim oMail As Outlook.MailItem
Dim strDASLFilter As String
Dim strScope As String

SendKeys "{HOME}", True
On Error GoTo Err_SearchFolderForSender
 
strFilter = InputBox("Enter Search String:", "Search")
If strFilter = "" Then
    Exit Sub
End If
 
' lets get the email address from a selected message
Set oMail = ActiveExplorer.Selection.Item(1)
If strFilter = "" Then Exit Sub
 
' From email address
Const From1 As String = "http://schemas.microsoft.com/mapi/proptag/0x0065001f"
Const From2 As String = "http://schemas.microsoft.com/mapi/proptag/0x0042001f"
 
strDASLFilter = """urn:schemas:httpmail:fromname"" LIKE '%" & strFilter & "%' " + _
"OR ""urn:schemas:httpmail:textdescription"" LIKE '%" & strFilter & "%' " + _
"OR ""urn:schemas:httpmail:displaycc"" LIKE '%" & strFilter & "%' " + _
"OR ""urn:schemas:httpmail:displayto"" LIKE '%" & strFilter & "%' " + _
"OR ""urn:schemas:httpmail:subject"" LIKE '%" & strFilter & "%' " + _
"OR ""urn:schemas:httpmail:thread-topic"" LIKE '%" & strFilter & "%' " + _
"OR ""http://schemas.microsoft.com/mapi/received_by_name"" LIKE '%" & strFilter & "%' " + _
"OR ""http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8586001f"" LIKE '%" & strFilter & "%' " + _
"OR ""http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/85a4001f"" LIKE '%" & strFilter & "%' " + _
"OR ""http://schemas.microsoft.com/mapi/id/{00062041-0000-0000-C000-000000000046}/8904001f"" LIKE '%" & strFilter & "%' " + "OR ""http://schemas.microsoft.com/mapi/proptag/0x0e03001f"" LIKE '%" & strFilter & "%' " + _
"OR ""http://schemas.microsoft.com/mapi/proptag/0x0e04001f"" LIKE '%" & strFilter & "%' " + _
"OR ""http://schemas.microsoft.com/mapi/proptag/0x0042001f"" LIKE '%" & strFilter & "%' " + "OR ""http://schemas.microsoft.com/mapi/proptag/0x0044001f"" LIKE '%" & strFilter & "%' " + _
"OR ""http://schemas.microsoft.com/mapi/proptag/0x0065001f"" LIKE '%" & strFilter & "%' "
  
strScope = "Inbox"
 
Set objSearch = Application.AdvancedSearch(Scope:=strScope, Filter:=strDASLFilter, SearchSubFolders:=True, Tag:="SearchFolder")
'Save the search results to a searchfolder
objSearch.Save (strFilter)
Set objSearch = Nothing
 
Exit Sub
 
Err_SearchFolderForSender:
MsgBox "Error # " & Err & " : " & Error(Err)
 
End Sub

Becoming a VBA expert isn't hard
Over 5000 members are improving their VBA skills for free with our email tutorials. Why don't you join them? Our experts share time-saving VBA tips and we'll give you access to our huge macro library - it's sure to speed up your macro development.

I want to get your free VBA material

Tutorial - VBA Outlook Search

  1. Enable Outlook Macros
  2. Open VBA Editor (Alt+F11)
  3. Paste OutlookSearch in new module
  4. Make sure you have an email selected in your box
    1. The SendKeys command is my attempt to do this for you, but if your inbox isn’t selected, you may still receive an error.
  5. Run OutlookSearch
    1. From Outlook, press Alt+F8
    2. Select OutlookSearch
    3. Press Run
  6. Type your search string in the InputBox
  7. Click OK

Outlook Search InputBox Prompt
Search Box

Your search results will show up under Search Folders in your Navigation Pane.

Outlook Search Folders Location
Search Folders

When you’re done with your Outlook Search Folders, you can delete them by right clicking and selecting “Delete Folder.” If it’s a search string you regularly search for, you can keep the folder and come back to it later.

If you want a shortcut to your new OutlookSearch macro, I recommend you add it to your outlook ribbon.

Application Ideas - VBA Outlook Search

The main idea here to enable some sort of Outlook email search functionality using VBA if your default Outlook search is not working. However, you could use this to create custom search folders to track emails from a particular person or emails discussing a certain project.

Comments

Share this article with a friend if you like what you see. Let me know if you outlook email search is still not working with this VBA script and I’ll be happy to help you troubleshoot. The comments section is where we all learn from each other.