Outlook 2016 related mails in outlook

Hariprasad.R

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
related mails in outlook, how we can get it from different folders (our chosen folders) using VBA code ? Example the search folders shall be inbox & its subfolders; and an another folder from outside inbox tree.
 

Hariprasad.R

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
related mails in outlook, how we can get it from different folders (our chosen folders) using VBA code ? Example the search folders shall be inbox & its subfolders; and an another folder from outside inbox tree.
 

Hariprasad.R

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
Can you pls advice on this request.

I am using the below code, which search inbox & all the subfolders under it. I wish to get results from the other folder too as marked (above Inbox tree)

-----------------------------------------------------------------------------------------------------------------------------

Dim myOlApp As New Outlook.Application

Dim strFrom, strSubject As String

Dim oMail As Outlook.MailItem

Dim txtSearch As String



Set oMail = GetCurrentItem()

strSubject = oMail.ConversationTopic

strFrom = oMail.SenderName

txtSearch = "[Conversation]:=""" & strSubject & """ (to:(" & strFrom & ") OR from:(" & strFrom & "))"



myOlApp.ActiveExplorer.Search txtSearch, olSearchScopeSubfolders

1656868568040.png
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
myOlApp.ActiveExplorer.Search txtSearch, olSearchScopeSubfolders
You need to change the scope to all - olSearchScopeAllFolders

If you use advanced search, you can name the folders you want to check.
 

Hariprasad.R

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
You need to change the scope to all - olSearchScopeAllFolders

If you use advanced search, you can name the folders you want to check.
Dear Diane,

Thanks, i have tried it (olSearchScopeAllFolders). But it provides result from all the opened folders in out look; Inbox & its sub-folders, Sent item folder & Archived folders.
Here I am looking search results from Inbox & its sub-folders, and from one another custom folder outside Inbox tree as below.
No need to include Sent item folder, Delete folder and not any other Archived folders if not specifically mentioned.
See example below. All black arrow folders need to search (olSearchScopeSubfolders will do this) And need to search the red arrow folder too in my present case.

1656962234376.png
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
what is your full code sample? I'll take a look at it.
 

Hariprasad.R

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
what is your full code sample? I'll take a look at it.
Dear Diane,

See below my full coding; It is working, but i need to add one another folder too as mentioned before.
Thanks.


Code:
Sub Search_Related()

Dim myOlApp As New Outlook.Application
Dim oMail  As Outlook.MailItem
Dim strFrom, strSubject As String
Dim txtSearch As String

Set oMail = GetCurrentItem()
strFrom = oMail.SenderName
strSubject = oMail.ConversationTopic

'Search crieria for the related mail search
txtSearch = "[Conversation]:=""" & strSubject & """ (to:(" & strFrom & ") OR from:(" & strFrom & "))"

'Search current folder & subfolders
myOlApp.ActiveExplorer.Search txtSearch, olSearchScopeSubfolders

Set myOlApp = Nothing
 
End Sub
 

Hariprasad.R

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
Dear Diane,

See below my full coding; It is working, but i need to add one another folder too as mentioned before.
Thanks.

Sub Search_Related()

Dim myOlApp As New Outlook.Application
Dim oMail As Outlook.MailItem
Dim strFrom, strSubject As String
Dim txtSearch As String

Set oMail = GetCurrentItem()
strFrom = oMail.SenderName
strSubject = oMail.ConversationTopic

'Search crieria for the related mail search
txtSearch = "[Conversation]:=""" & strSubject & """ (to:(" & strFrom & ") OR from:(" & strFrom & "))"

'Search current folder & subfolders
myOlApp.ActiveExplorer.Search txtSearch, olSearchScopeSubfolders

Set myOlApp = Nothing

End Sub
1656964158274.png
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
since you are searching by conversation, outlook's conversation view would show all messages in the conversation... otherwise, you need to use advanced search.
 

Hariprasad.R

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
Dear Diane,

Thanks for your inputs.

1) With my existing code shared, how can i change/use advanced search to get results from different chosen folders ?
Kindly advice. (option-1 : Search Related method)

2) I have done it using your suggested other method (Option-2 : By creating outlook search folder method.)
Here i need your help on two sessions.

a) exact subject search instead of "like"
b) on selecting any mail & running the macro, i need to get the result in the same "search folder". Same folder always & results based on the selected mail item. Here 1st time i am getting correct result, but 2nd time (on selecting diff mail item & running the code) getting error as there is already the same search folder existing. Here i need to update the search folder with the new result instead of creating new Search Folder. Given below the related code sessions i have prepared based on your shared data/module link.

Code:
Sub SearchFolder_ForConversation()

On Error GoTo Err_SearchFolderForSender

Dim strFrom As String
Dim strTo As String
Dim strSubject As String
Dim oMail As Outlook.MailItem

Set oMail = ActiveExplorer.Selection.Item(1)

strFrom = oMail.SenderEmailAddress
strTo = oMail.SenderName
strSubject = oMail.ConversationTopic

If strFrom = "" Then Exit Sub

Dim strDASLFilter As String

' From & To fields
Const From1 As String = "http://schemas.microsoft.com/mapi/proptag/0x0065001f"
Const From2 As String = "http://schemas.microsoft.com/mapi/proptag/0x0042001f"
Const To1 As String = "http://schemas.microsoft.com/mapi/proptag/0x0e04001f"
Const To2 As String = "http://schemas.microsoft.com/mapi/proptag/0x0e03001f"

strDASLFilter = "urn:schemas:httpmail:subject LIKE '%" & strSubject & "%'" & _
" AND ((""" & From1 & """ CI_STARTSWITH '" & strFrom & "' OR """ & From2 & """ CI_STARTSWITH '" & strFrom & "')" & _
" OR (""" & To1 & """ CI_STARTSWITH '" & strFrom & "' OR """ & To2 & """ CI_STARTSWITH '" & strFrom & "' OR """ & To1 & """ CI_STARTSWITH '" & strTo & "' OR """ & To2 & """ CI_STARTSWITH '" & strTo & "' ))"

Debug.Print strDASLFilter

Dim strScope As String
strScope = "'Inbox', 'eeminders'"

Dim objSearch As Search
Set objSearch = Application.AdvancedSearch(Scope:=strScope, Filter:=strDASLFilter, SearchSubFolders:=True, Tag:="SearchFolder")

'Save the search results to a searchfolder named "Search Result"
objSearch.Save ("Search Result")

Set objSearch = Nothing
MsgBox "Done"

Exit Sub

Err_SearchFolderForSender:
MsgBox "Error # " & Err & " : " & Error(Err)

End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
'urn:schemas:httpmail:subject' = '" & strSubject & "'"


on updating the search folder - I don't think you can, but wuill check. I have a code to delete the folder if it exists. Will see if we can just update the filter.


A cheat to get the SQL - open View Settings and create a filter on the advanced tab then copy the sql from the SQL tab.

1657051709504.png


1657051699665.png
 

Hariprasad.R

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server
Dear Diane,

I am not getting the exact search with the above code (but error, may be syntax is some other), kindly advise whether it works fine for you.
Updating the search folder or the code to delete the folder if it exists, can you pls recommend the code to use.

Thanks,
Hari
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
It is working at all? It works here - I had to change "eeminders" to Archive to test it. If you didn't fix the spelling in your code, that could be the problem.

You won't be able to update a search folder - you can either delete it and recreate it or use a value in the selected message to name the folder.

This seems the best option if multiple people are in the thread and you are limiting it to that subject.
objSearch.Save (strSubject)
 
Similar threads
Thread starter Title Forum Replies Date
L Email with correct To address but displaying name of a related person Using Outlook 0
E Search for folder by key in subject then move new message to related folder Outlook VBA and Custom Forms 1
B Find related messages to sender Outlook VBA and Custom Forms 7
B Find Related Emails to sender Using Outlook 1
K Different kind of issue related to creation of archive after one has been deleted Exchange Server Administration 1
G How to let data in an account user defined field appear in the same field all related opportunities BCM (Business Contact Manager) 1
F Disable "Find related messages" Using Outlook 1
D Questions related to usage of webpage in outlook Using Outlook 1
W outlook refuses to open any attachments ... not related to shadow directory Using Outlook 2
C Related Messages Option Using Outlook 1
D Outlook 2007 Find All Related and Messages From Sender not working Using Outlook 3
S Couple of questions related to calender Using Outlook 1
C Change the related contect diplay columns BCM (Business Contact Manager) 4
T How to show related Opportunities on Accounts form BCM (Business Contact Manager) 4
T Completely wipe all Outlook-related info for reinstall Using Outlook 1
F Excel VBA to move mails for outlook 365 on secondary mail account Outlook VBA and Custom Forms 1
T E-mails marked as read turn back to unread after a couple of seconds Using Outlook 1
M Outlook 2010 How could I globally redesign an outlook template form/region/inspector template used to display mail lists or an individual mails? Outlook VBA and Custom Forms 0
L Macro to add Date & Time etc to "drag to save" e-mails Outlook VBA and Custom Forms 17
S Outlook Macro to send auto acknowledge mail only to new mails received to a specific shared inbox Outlook VBA and Custom Forms 0
Eike Move mails via macro triggered by the click of a button? Outlook VBA and Custom Forms 0
Terry Sullivan E-Mails Sent Using a Group Box Result in 70 Kickbacks Using Outlook 4
T Search for incoming e-mails for a specified time range Using Outlook 1
F Outlook 2016 Junk E-Mails Using Outlook 5
RBLampert Invisible e-mails in Gmail accounts Using Outlook 0
P when i move inbox mails to another folder in outlook the mail disappears Using Outlook 1
I How to receive mails on behalf of when sent to group mail? Using Outlook 2
A Script to fetch data from mails in restricted collection and sending them to excel Using Outlook 1
O Having rules run on old mails noved to inbox Outlook VBA and Custom Forms 8
R Would creating a new profile cause Outlook to download all the old mails from the server? Using Outlook 1
C VBA to Forward e-mails from certain address and between certain times Outlook VBA and Custom Forms 1
O Copy mails from many subfolders to 1 foldr Using Outlook 2
S Outlook [Online - Office365] perfomance is getting affected when accessing the mails using Redemptio Using Outlook 1
F Move mails from Deleted Items folder back to its original folder where the mails got deleted Using Outlook 0
Thiago Manzano Copying E-mails to a folder on HD Using Outlook 1
A Processing Incoming E-mails with Macros Using Outlook 0
O Outlook 2013 Problems sending large mails Using Outlook 4
P Send mails by Gmail not shown in outlook Using Outlook 5
E How to find or recover Lost e-mails within a folder under Inbox? Using Outlook 2
Karsten V invites from outside company arrives as regular mails Using Outlook 2
A Encrypting mails depending from the address Outlook VBA and Custom Forms 0
Yusufbodrum Highlighting selected mails in any folder Using Outlook 2
C Linking e-mails BCM (Business Contact Manager) 1
S Mails getting downloaded multiple times Using Outlook 1
K Macro doesn't recognize local mails from colleagues Outlook VBA and Custom Forms 2
I Delete e-mails from server Using Outlook 2
W URGENT Cancel and remove imported pop3 mails Using Outlook 6
T Cannot get pop3 mails to my Android and Outlook Exchange Server Administration 1
A 1 folder to see all new mails Using Outlook 1
Z OL2007 - is there a way to disable the "feature" that cripples mails that are in the "junk" folder? Using Outlook 37

Similar threads

Top