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.
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
- Open the VBA editor using Alt+F11
- Right click on Project1 and choose Insert > Module
- Paste the following code into the module.
- Set macro security to low to test the code, then use Selfcert to sign the macro before change security to high.
- Add the macro to a ribbon, QAT, or toolbar button.
- 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.
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....
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.)
https://www.slipstick.com/developer/code-samples/save-selected-message-file/
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
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.)
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 :-)
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.
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)?
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.
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 »
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
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.
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.
https://www.slipstick.com/developer/outlook-vba-work-with-open-item-or-select-item/
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.
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.
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
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?
You need to replace this:
Set oContact = ActiveExplorer.Selection.item(1)
with
Set objItem = GetCurrentItem()
and get the function from GetCurrentItem.