Friday, January 14, 2022

Outlook emails Filter by email ID

Hello Guys, 

Here is a VBA code to search inbox in Outlook by any Email ID of a person. 

If you like you can also create a button on ribbon in outlook and assign this code.

Sub SearchByEmailID()
    Dim olApp As Outlook.Application
    Dim ns As Outlook.namespace
    Dim sSearchMail As String
    Dim oSearchFolder As Outlook.Folder
    Dim sSearchCriteria As String
    
    Dim strFilter As String
    Dim oContact  As Outlook.ContactItem
    Dim iFile As Integer
    Dim sInp As String
    
    Set olApp = New Outlook.Application
    Set ns = olApp.GetNamespace("MAPI")
    
    sSearchMail = "[email protected]" 'Change EmailID here.
     
    Set oSearchFolder = ns.GetDefaultFolder(olFolderInbox)
    Set olApp.ActiveExplorer.CurrentFolder = oSearchFolder
    
    sSearchCriteria = "from:" & sSearchMail
    olApp.ActiveExplorer.Search sSearchCriteria, olSearchScopeAllFolders
    
    Set olApp = Nothing
End Sub


How to Install and Use the Macro:
In order to use the macro, Open Outlook and Press Alt +F11 key. This will open the VBA editor in outlook.

Go to Insert menu and insert a module and paste the above code and Press save button and close the window.

Now Go to Outlook and and Press Alt+F8 key. 

You will see a macro named SearchByEmailID. Press the Run button. This will filter the inbox with the Email ID.

If you like this article or have any query/suggestions, please share your feedback in the comments section below..

Thanks,
Ashwani

 

I am starting a weekend VBA training course from 06 Jan 2024. If you are interested, please register yourself and go through the course details. Here is the link below: 


If you want to develop macro for any Microsoft office product
or
automate your task in excel please reach out to my Fiverr account, mentioned below:

0 comments: