Link to home
Start Free TrialLog in
Avatar of Giovanni A
Giovanni A

asked on

Create Search folder in VBA

Search folder are a very cool tool but difficult to use. We have exchange on-premises 2016 and outlook 2016 professional.

I would like to write a VBA to: add in mail context menu the option: "create search folder #senderdomain". This should Create a search folder called #domain including all the email sent from and to that domain.

I found that code:
Sub Application_ItemContextMenuDisplay(ByVal CommandBar As Office.CommandBar, ByVal Selection As Selection)
    Dim objCommandBarButton As Office.CommandBarButton
 
    'Add "New Search Folder" to the ContactItem's Context Menu
    If (Selection.Count = 1) And (Selection.Item(1).Class = olContact) Then
       Set objCommandBarButton = CommandBar.Controls.Add(msoControlButton)
 
       With objCommandBarButton
           .Style = msoButtonIconAndCaption
           .Caption = "New Search Folder"
           .FaceId = 1744
           .OnAction = "Project1.ThisOutlookSession.CreateRelatedSearchFolder"
       End With
    End If
End Sub

Sub CreateRelatedSearchFolder()
    Dim objContact As Outlook.ContactItem
    Dim strEmailAddress As String
    Dim strDisplayName As String
    Dim strFilter As String
    Dim strFrom1, strFrom2, strTo1, strTo2 As String
    Dim strScope As String
    Dim objSearch As Outlook.Search
 
    'Get the selected contact
    Set objContact = Application.ActiveExplorer.Selection.Item(1)
    'Get the contact's main email address and display name
    strEmailAddress = objContact.Email1Address
    strDisplayName = objContact.Email1DisplayName
 
    If strEmailAddress = "" Then
       Exit Sub
    End If
 
    strFrom1 = "http://schemas.microsoft.com/mapi/proptag/0x0065001f"
    strFrom2 = "http://schemas.microsoft.com/mapi/proptag/0x0042001f"
    strTo1 = "http://schemas.microsoft.com/mapi/proptag/0x0e04001f"
    strTo2 = "http://schemas.microsoft.com/mapi/proptag/0x0e03001f"
 
    'Specify the search filter
    strFilter = "((""" & strFrom1 & """ CI_STARTSWITH '" & strEmailAddress & "' OR """ & strFrom2 & """ CI_STARTSWITH '" & strEmailAddress & "')" & " OR (""" & strTo1 & """CI_STARTSWITH '" & strEmailAddress & "' OR """ & strTo2 & """ CI_STARTSWITH '" & strEmailAddress & "' OR """ & strTo1 & """ CI_STARTSWITH '" & strDisplayName & "' OR """ & strTo2 & """ CI_STARTSWITH '" & strDisplayName & "' ))"
 
    'Specify the folders to be searched
    strScope = "'" & Application.Session.GetDefaultFolder(olFolderInbox).FolderPath & "','" & Application.Session.GetDefaultFolder(olFolderSentMail).FolderPath & "'"
 
    'Search all mails from or to the specific contact
    Set objSearch = Application.AdvancedSearch(strScope, strFilter, True, "SearchFolder")
 
    'Save the search results to a search folder
    objSearch.Save (objContact.FullName)
End Sub

Open in new window


It does somethink somehow similar but I can't understand the syntax. Can someone help me or give me some link to learn how to do so?

Thank you so much
ASKER CERTIFIED SOLUTION
Avatar of Neil Fleming
Neil Fleming
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Giovanni A
Giovanni A

ASKER

Thankyou. How can I fix the rest of the code?
which other code? Do you mean the first routine which adds the button?
Ok, this works good!  Thankyou  I still need
1) add the button
2) I want to start from the recived email not from the contact saved (if is possible)