• Outlook User
  • Exchange Admin
  • Office 365
  • Outlook Developer
  • Outlook.com
  • Outlook Mac
  • Common Problems
    • Outlook BCM
    • Utilities & Addins
    • Video Tutorials
    • EMO Archives
    • Outlook Updates
    • Outlook Apps
    • Outlook & iCloud Issues
    • Forums

Use Instant search to find messages from a contact

Slipstick Systems

› Developer › Use Instant search to find messages from a contact

Last reviewed on October 25, 2015   —  21 Comments

Applies to: Outlook 2013, Outlook 2010, Outlook 2007

January 7, 2013 by Diane Poremsky 21 Comments

Older versions of Outlook have the Activities tab which will find all Outlook items associated with a particular contact. Activities are broken in Outlook 2010 and removed from Outlook 2013.

While you can use the Social Connector's People pane to see all messages, appointments, and attachments from a person, you can also use Instant Search. Type from:(name) or from:(email@address) in the Instant search field.

Instant Search field

This macro makes it easier to use Instant Search to find messages for a selected contact, by entering the contact's name or email address into the Instant Search field in the Inbox.

If you need persistent search results, see How to create an Outlook search folder using VBA.

This macro can use any valid Instant search criteria. You just need to replace or edit the txtSearch line. To find messages to and from a contact, use txtSearch = "from:" & strFilter & " OR to:" & strFilter

Search for messages from contact macro

  1. Open the VBA editor using Alt+F11
  2. Right click on Project1 and choose Insert > Module
  3. Paste the following code into the module.
  4. Set macro security to low to test the code, then use Selfcert to sign the macro before change security to high.
  5. Add the macro to a ribbon, QAT, or toolbar button.
  6. Select a contact and run the macro.

Sub SearchByAddress()

Dim myOlApp As New Outlook.Application
Dim ns As Outlook.NameSpace
Dim strFilter As String
Dim oContact  As Outlook.ContactItem

Set ns = myOlApp.GetNamespace("MAPI")

Set oContact = ActiveExplorer.Selection.item(1)

' use oContact.FullName to search on the name
strFilter = oContact.Email1Address
 
Set myOlApp.ActiveExplorer.CurrentFolder = ns.GetDefaultFolder(olFolderInbox)

txtSearch = "from:" & strFilter
myOlApp.ActiveExplorer.Search txtSearch, olSearchScopeAllFolders

Set myOlApp = Nothing

End Sub

Search for messages to or from a sender

This version of the code creates an instant search for all messages to or from the sender, including messages you sent.

To use, select a message and run the macro. An instant search for messages to or from the person's email address is created.

Sub SearchByAddress()
 
Dim myOlApp As New Outlook.Application
Dim ns As Outlook.NameSpace
Dim strFilter As String
Dim oMail  As Outlook.MailItem
 
Set ns = myOlApp.GetNamespace("MAPI")
 
Set oMail = Application.ActiveExplorer.Selection.Item(1)
      strFilter = oMail.SenderEmailAddress
  
Set myOlApp.ActiveExplorer.CurrentFolder = ns.GetDefaultFolder(olFolderInbox)
     txtSearch = "from:(" & Chr(34) & strFilter & Chr(34) & ") OR to:(" & Chr(34) & strFilter & Chr(34) & ")"
     myOlApp.ActiveExplorer.Search txtSearch, olSearchScopeAllFolders
 
Set myOlApp = Nothing
 
End Sub

 

Search for All Contact Addresses

This version of the macro search for messages from all three email addresses on a contact, if additional addresses exist.

Sub SearchByAddress()
 
Dim myOlApp As New Outlook.Application
Dim ns As Outlook.NameSpace
Dim strFilter As String
Dim oContact  As Outlook.ContactItem
 
Set ns = myOlApp.GetNamespace("MAPI")
 
Set oContact = ActiveExplorer.Selection.Item(1)
 
' use oContact.FullName to search on the name
strFilter = Chr(34) & oContact.Email1Address & Chr(34)

If oContact.Email2Address <> "" Then
strFilter = strFilter & " OR " & Chr(34) & oContact.Email2Address & Chr(34)
End If

If oContact.Email3Address <> "" Then
strFilter = strFilter & " OR " & Chr(34) & oContact.Email3Address & Chr(34)
End If
  
Set myOlApp.ActiveExplorer.CurrentFolder = ns.GetDefaultFolder(olFolderInbox)

txtSearch = "from:(" & strFilter & ") OR to:(" & strFilter & ")"
myOlApp.ActiveExplorer.Search txtSearch, olSearchScopeAllFolders
 
Set myOlApp = Nothing
 
End Sub

Search Journal

If you are still using Journal, you can tweak the macro to search the journal for items associated with the contact. Change the contact field to Full name and the folder to journal.

strFilter = oContact.FullName
Set myOlApp.ActiveExplorer.CurrentFolder = ns.GetDefaultFolder(olFolderJournal)

' change the search filter to look for the contact name
txtSearch = "contactnames:(" & strFilter & ")"

Valid Search Scopes

Outlook 2010 and Outlook 2013 use the following search scopes:

Scope Description
olSearchScopeCurrentFolder Limit the search to the currently selected folder.
olSearchScopeSubfolders Limit the search to the currently selected folder and its subfolders. To search all folders in one data file, select the top level of the pst.
olSearchScopeAllFolders Search all folders (of the current folder type). This search includes all data stores that are enabled for search.
olSearchScopeAllOutlookItems Search all Outlook items in all folders in stores that are enabled for search.

Outlook 2007 users are are limited to olSearchScopeAllFolders and olSearchScopeCurrentFolder.

More Information

How to Create a Unified Inbox View
How to create an Outlook search folder using VBA
Instant Search Queries

Use Instant search to find messages from a contact was last modified: October 25th, 2015 by Diane Poremsky
  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Google+ (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Share on Skype (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to print (Opens in new window)

Related Posts:

  • How to Create a Unified Inbox View
  • Find messages in a conversation
  • Use VBA to create an Outlook Search Folder for Sender
  • Search for All Messages from Contact and Display in New Window

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.

Leave a Reply

21 Comments on "Use Instant search to find messages from a contact"

2500
Photo and Image Files
 
 
 
Audio and Video Files
 
 
 
Other File Types
 
 
 
2500
Photo and Image Files
 
 
 
Audio and Video Files
 
 
 
Other File Types
 
 
 

  Subscribe  
newest oldest most voted
Notify of
sapana
sapana
Share On TwitterShare On Google

This code was really helpful for me. thank you for posting it here.
I need go one more step ahead, to copy the search result emails onto the hard drive.
I would really appreciate if someone could help me....

Vote Up00Vote Down Reply
July 5, 2017 12:22 pm
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

The easiest way, if you don't need to do this "all the time" is to select all and copy (ctrl+a, c) then paste into a folder on the hard drive.

If you need to use a macro, you'll need to get a count of messages then use a loop to work through them - once you do that, saving is easy. (You will probably need to walk every folder and check the recipients - I'm not sure if you can loop the search results.)

Save Selected Email Message as .msg File

Vote Up00Vote Down Reply
July 7, 2017 12:45 am
Michael
Michael
Share On TwitterShare On Google

How can I show all tasks associated with a contact?

I changed the macro to use:
txtSearch = "contactnames:(" & strFilter & ")"

but it will show no results

the full code:
Sub SearchByAddress()

Dim myOlApp As New Outlook.Application
Dim ns As Outlook.NameSpace
Dim strFilter As String
Dim oContact As Outlook.ContactItem

Set ns = myOlApp.GetNamespace("MAPI")

Set oContact = ActiveExplorer.Selection.Item(1)

' use oContact.FullName to search on the name
strFilter = oContact.FullName

Set myOlApp.ActiveExplorer.CurrentFolder = ns.GetDefaultFolder(olFolderTasks)

txtSearch = "contactnames:(" & strFilter & ")"
myOlApp.ActiveExplorer.Search txtSearch, olSearchScopeAllFolders

Set myOlApp = Nothing

End Sub

Vote Up00Vote Down Reply
February 27, 2017 5:52 am
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

Fails here too, using contact and contacts too - i know they broke some aspects of the contact linking feature and if they are indexing that field search won't work in Tasks. (The contact search should work for mail.)

Vote Up00Vote Down Reply
March 4, 2017 1:44 am
Henrik
Henrik
Share On TwitterShare On Google

Thanks for posting all these great code examples. I have tried to get the "Search for messages to or from a sender" variant to work, but the search string coming out of the macro looks very strange. The strFilter string starts with /O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP...? (I am running Outlook 2013). Thanks in advance for your help :-)

Vote Up00Vote Down Reply
November 29, 2016 2:35 pm
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

That is the Exchange x.500 address - it's only used internally. In Rules, you'd look for "/OU" in the address... not sure if it will work with instant search.

Vote Up00Vote Down Reply
March 4, 2017 1:37 am
Hannah
Hannah
Share On TwitterShare On Google

I'm not sure if this is where I should put this or not -- but I'm looking to do a global find in outlook for a category -- across tasks & email. Is that possible, or do I have to do two searches (one in mail and another in tasks)?

Vote Up00Vote Down Reply
March 17, 2016 3:05 pm
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

One of the instant search scopes is all outlook items - that will include calendar and contacts, but if the category isn't used there, it won't find anything. It is is possible to include a message class, which would limit it to tasks and mail.

Vote Up00Vote Down Reply
March 18, 2016 7:50 am
JBG
JBG
Share On TwitterShare On Google
Diane, Where in this program should I change or add the Set oContact command line you recommend? Please review and modify how this code should read to get the button to wok on an Opened Outlook Contact Form: Sub FindContactActivities() If Application.Session.DefaultStore.IsInstantSearchEnabled Then Dim olkExplorer As Outlook.Explorer Set olkExplorer = Application.Explorers.Add(Application.Session.GetDefaultFolder(olFolderInbox), olFolderDisplayNormal) Dim oItem As Object Set oItem = Application.ActiveInspector.CurrentItem If oItem.Class = olContact Then Dim myContact As Outlook.ContactItem Set myContact = oItem Dim myContactAddress As String Dim myContactName As String myContactAddress = myContact.Email1Address myContactName = myContact.FullName Dim olkFilter As String 'Linked Contacts olkFilter = "contactnames:(" & Chr(34) & myContactAddress & Chr(34) & " OR " & Chr(34) & myContactName & Chr(34) & ")" 'From this contact olkFilter = olkFilter & " OR " & "from:(" & Chr(34) & myContactAddress & Chr(34) & " OR " & Chr(34) & myContactName & Chr(34) & ")" 'To this contact olkFilter = olkFilter & " OR " & "to:(" & Chr(34) & myContactAddress & Chr(34) & " OR " & Chr(34) & myContactName & Chr(34) & ")" Call olkExplorer.Search(olkFilter, olSearchScopeAllOutlookItems) Call olkExplorer.Display Else MsgBox "Please run this command from an opened Contact item.", vbExclamation, "Open a Contact item" End If Set olkExplorer =… Read more »
Vote Up00Vote Down Reply
October 9, 2015 8:30 am
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

your code uses oitem as the contact object, not ocontact, and already uses the current item:
Set oItem = Application.ActiveInspector.CurrentItem

in my code, you'd change
Set oContact = ActiveExplorer.Selection.item(1)
to
Set oContact = Application.ActiveInspector.CurrentItem

The code works here. it's hard to say what is wrong.
Video of my macro in action is here (when its finished processing) - https://drive.google.com/file/d/0B22iPSInt7uxYmZGMHJUZVJMWVk/view?usp=sharing

Vote Up00Vote Down Reply
October 9, 2015 10:21 am
JBG
JBG
Share On TwitterShare On Google

Diane,

I left this question on another page already so forgive the duplication.
I have the search macro done and it works in the VBA Editor Run function, but the button I added to the Contact Screen ribbon and QAT do not work. Nothing at all seems to be happening. I did a self certificate for the VBA, but I also tried removing Marco security and nothing has fixed it.

Vote Up00Vote Down Reply
October 6, 2015 9:42 pm
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

It works with selected contacts as written, not opened contacts. It should work if the button is on the main outlook ribbon or QAT. Changing the line to Set oContact = myOlApp.ActiveInspector.CurrentItem will make it work with an open contact.

Outlook VBA: Work with Open Item or Selected Item

Vote Up00Vote Down Reply
October 6, 2015 10:01 pm
Kay C
Share On TwitterShare On Google

HI, Can you make your macro work for all the email addresses on record in the contact instead of just the first one? Plus, can you make it show all sent AND received emails from all the contact's email addresses. This would truly mimic the defunct "show activities" feature.

Vote Up00Vote Down Reply
November 26, 2014 12:24 pm
Diane Poremsky
Share On TwitterShare On Google

All addresses: Yes. This line needs edited - strFilter = oContact.Email1Address - to include all fields (email2address, email3address)
strfilter = chr(34) & oContact.Email1Address & chr(34) & " OR " & chr(34) & ocontact.email2address & chr(34) & " OR " & chr(34) & ocontact.email3address & chr(34)
txtSearch = "from:(" & strFilter & ")"

the chr(34) adds double quotes so mary@domain.com doesn't find all instances of mary, domain, and com. if you want to search both from and body, remove From: from the search string.

Vote Up00Vote Down Reply
November 26, 2014 12:40 pm
Diane Poremsky
Share On TwitterShare On Google

As an FYI, i updated the page to include a macro that uses all 3, if the fields contain an address. https://www.slipstick.com/developer/instant-search-messages-selected-contact/#all3

Vote Up00Vote Down Reply
November 26, 2014 5:55 pm
tomaszek926
tomaszek926
Share On TwitterShare On Google

Hi,
Great macro, great article.
Especially, as as a user of IMAP accounts, i cannot use Activities - i use ONLY other pst data file, then default...
Really easy to implement and modify.
I have adapted it to Polish and to searching To: OR From: ....

One remark and question.
It works only while i check a contact on contact list.
Would be nice to have it also working when i open a contact form - sth like it was in O2007 (Activities - e-mail)
And best would be also to have it working on the list of e-mails (Inbox, for example).
I mean: i check an e-mail, and starting the macro i can have full history of e-mail communication with checked e-mail sender.....

Is it possible?

Vote Up00Vote Down Reply
June 19, 2013 2:19 am
Diane Poremsky
Share On TwitterShare On Google

You need to replace this:
Set oContact = ActiveExplorer.Selection.item(1)

with

Set objItem = GetCurrentItem()

and get the function from GetCurrentItem.

Vote Up00Vote Down Reply
June 19, 2013 5:04 am

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

Latest EMO: Vol. 23 Issue 10

Subscribe to Exchange Messaging Outlook






Our Sponsors

  • Popular
  • Latest
  • Week Month All
  • Adjusting Outlook's Zoom Setting in Email
  • This operation has been cancelled due to restrictions
  • How to Remove the Primary Account from Outlook
  • Outlook is Not Recognized as the Default Email Client
  • Pictures Don't Display in Outlook Messages
  • To Cc or Bcc a Meeting Request
  • The Signature or Stationery and Fonts button doesn't work
  • Remove a password from an Outlook *.pst File
  • Understanding Outlook's Auto-Complete Cache (*.NK2)
  • Exchange Account Set-up Missing in Outlook 2016
  • iCloud error: Outlook isn't configured to have a default profile
  • Setting Custom Reminder Times
  • Add Additional Addresses to Room Mailboxes
  • Create a Task from a Message and include the Attachment
  • Forward email messages by date
  • Open multiple Outlook windows when Outlook starts
  • Office 365 Fraud Detection Checks
  • Outlook Request: Calendar Details View
  • Outlook's "Not Junk" option isn't available
  • Outlook Tip: Show all Mondays in the Calendar
Ajax spinner

Newest VBA Samples

Open multiple Outlook windows when Outlook starts

Set most frequently used Appointment Time Zones

How to change the From field on incoming messages

VBA: File messages by client code

Update Contact Area Codes

Set a reminder on selected items in the To-Do List

Replicate GTD: Create a task after sending a message

Use VBA to read fields in attached messages

Move Outlook Folders using VBA

Replicate Smart Lookup using a macro

Recent Bugs List

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

Windows 10 Issues

  • iCloud, Outlook 2016, and Windows 10
  • Better Outlook Reminders?
  • Coming Soon to Windows 10: Office 365 Search
  • Outlook Links Won’t Open In Windows 10
  • BCM Errors after Upgrading to Windows 10
  • Outlook can’t send mail in Windows 10: error Ox800CCC13
  • Missing Outlook data files after upgrading Windows?

Outlook 2016 Top Issues

  • The Windows Store Outlook App
  • Emails are not shown in the People Pane (Fixed)
  • Calendars aren’t printing in color
  • The Signature or Stationery and Fonts button doesn’t work
  • Outlook’s New Account Setup Wizard
  • BCM Errors after October 2017 Outlook Update
  • Excel Files Won’t Display in Reading Pane
  • Outlook 2016: No BCM
  • Exchange Account Set-up Missing in Outlook 2016

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

Outlook-tips.net Samples

VBOffice.net samples

OutlookCode.com

SlovakTech.com

Outlook MVP David Lee

MSDN Outlook Dev Forum

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
  • “Live” Group Calendar Tools

Convert to / from Outlook

  • Converting Messages and Calendar or
    Address books
  • Moving Outlook to a New Computer
  • Moving Outlook 2010 to a new Windows computer
  • Moving from Outlook Express to Outlook

Recover Deleted Items

  • Recover deleted messages from .pst files
  • Are Deleted Items gone forever in Outlook?

Outlook 2013 Absolute Beginner's Guide

Diane Poremsky [Outlook MVP]

Make a donation

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

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

Outlook Suggestion Box (UserVoice)

Slipstick Support Services

Contact Tools

Data Entry and Updating

Duplicate Checkers

Phone Number Updates

Contact Management Tools

Sync & Share

Share Calendar & Contacts

Synchronize two machines

Sharing Calendar and Contacts over the Internet

More Tools and Utilities for Sharing Outlook Data

Access Folders in Other Users Mailboxes

View Shared Subfolders in an Exchange Mailbox

"Live" Group Calendar Tools

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

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

You are going to send email to

Move Comment