I currently have an Outlook VBA macro that generated a search folder that filters emails I did not respond to. I receive up to 150 emails in a day and sometimes I read an email, but don't respond because I get side-tracked with other tasks, so this seemed like a good idea to keep things from slipping through the cracks. Unfortunately, my real goal was to have that search folder only show conversations where I have not replied (i.e. the last email in the conversation is not one I sent) and instead it shows ALL emails that I have not directly replied to, so the search filter will show a conversation if there is even a single email within that entire convsersation where I did not specifically reply to it. For example, if there are multiple people on an email chain and 2 people send emails within the conversation and then I send a reply to the latest email in that conversation, I technically did not reply to both emails individually, and therefore the conversation shows up in this search folder. I think it's a simple change to the filter, but I have no idea what to add/change to set this right.
Here is the code I used to create the search folder:
Sub CreateSearchFolder_AllNotRepliedEmails()
Dim strScope As String, strRepliedProperty As String, strFilter As String, strSaveName As String
Dim objSearch As Outlook.Search
'Specify the folders to be searched
'Here we use Inbox folder for an example
strScope = "'" & Application.Session.GetDefaultFolder(olFolderInbox).FolderPath & "'"
'Search filter
strRepliedProperty = "http://schemas.microsoft.com/mapi/proptag/0x10810003"
strFilter = Chr(34) & strRepliedProperty & Chr(34) & " <> 102" & " AND " & Chr(34) & strRepliedProperty & Chr(34) & " <> 103"
Set objSearch = Outlook.Application.AdvancedSearch(Scope:=strScope, Filter:=strFilter, SearchSubFolders:=True, Tag:="SearchFolder")
'Debug.Print strFilter
'Save the search folder
strSaveName = InputBox("Please enter the Search Folder title to use:")
If strSaveName = vbNullString Then
MsgBox "Operation canceled."
Exit Sub
End If
objSearch.Save (strSaveName)
MsgBox "Search folder is created successfully!", vbInformation + vbOKOnly, "Search Folder"
End Sub
The AdvancedSearch
method can't filter conversations that way. The best what you could do is to retrieve all non-replied items (which is actually what you are doing now). To recognize whether that is the most recent item in the conversation or not you need to do that manually by using the MailItem.GetConversation method which obtains a Conversation object that represents the conversation to which this item belongs.
When the advanced search is finished you can check whether a particular item is the latest in the conversion where it belongs to. Use the AdvancedSearchComplete event to determine when a given search has completed.