Re: Use Instant search to find messages from a contact

Status
Not open for further replies.

KoolPal

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
My set up is OL 2010 in an exchange environment.

I learnt / tweaked from the following:

Search for messages from contact macro

and ended with this:

Code:
Sub SearchMG() 
 
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 myOlApp.ActiveExplorer.CurrentFolder = ns.GetDefaultFolder(olFolderInbox) 
     txtSearch = "from:(Email name) OR  to:(Email name) OR cc:(Email name) AND  followupflag:followup flag" 
     myOlApp.ActiveExplorer.Search txtSearch, olSearchScopeAllFolders 
 
Set myOlApp = Nothing 
 
End Sub

I am trying to save this as a Search Folder. Is that possible?

Any other way to create a search folder using Instant Search logic?
 
You can create a search folder using VBA - I have a sample here that creates one for the selected message sender.
http://www.slipstick.com/developer/create-an-outlook-search-folder-using-vba/

Basically, you need to add this to your code, tweaked a little of course. strFilter is your txtSearch but i'm not 100% sure your string will work - try it and see. If not, you need to use DASL filters (samples at the bottom of that page).

Code:
Dim objSearch As Search 
Set objSearch = Application.AdvancedSearch(Scope:=strScope, Filter:=strDASLFilter, SearchSubFolders:=True, Tag:="SearchFolder") 
'Save the search results to a searchfolder 
objSearch.Save (strFilter) 
Set objSearch = Nothing
 
Thanks! I modified my code as below, but I get an error message :(

Code:
Sub SearchMG() 
 
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 objSearch = Application.AdvancedSearch(Scope:=strScope, Filter:=strDASLFilter, SearchSubFolders:=True, Tag:="SearchFolder") 
Set myOlApp.ActiveExplorer.CurrentFolder = ns.GetDefaultFolder(olFolderInbox) 
     strFilter = "from:(Email Name) OR  to:(Email Name) OR cc:(Email Name) AND  followupflag:followup flag" 
     myOlApp.ActiveExplorer.Search txtSearch, olSearchScopeAllFolders 
 
objSearch.Save (strFilter) 
Set objSearch = Nothing 
 
Set myOlApp = Nothing 
 
End Sub
 
Do you want to search only the Inbox for messages from the sender of the selected message or do you want to search the entire data file?

This will create a search folder for all messages to/cc or from a person that are in the Inbox. If the folder exists, it'll error so the On Error Resume Next ends it gracefully.

Code:
Sub SearchMGm() 
Dim oMail  As Outlook.MailItem 
 
Dim strFilter As String 
Dim strScope As String 
Dim strDASLFilter As String 
Set oMail = ActiveExplorer.Selection.Item(1) 
    
 
strFilter = oMail.SenderName 
strScope = "Inbox" 
 
strDASLFilter = "((" & Chr(34) & "urn:schemas:httpmail:displaycc" & Chr(34) & " LIKE '%" & strFilter & "%' OR " & Chr(34) & "urn:schemas:httpmail:fromname" & Chr(34) & " LIKE '%" & strFilter & "%' OR " & Chr(34) & "urn:schemas:httpmail:displayto" & Chr(34) & "  LIKE '%" & strFilter & "%'))" 
 
On Error Resume Next 
Set objSearch = Application.AdvancedSearch(Scope:=strScope, filter:=strDASLFilter, SearchSubFolders:=True, Tag:="SearchFolder") 
objSearch.Save (strFilter) 
Set objSearch = Nothing 
 
Set myOlApp = Nothing 
End Sub
 
If you want to search both the inbox and sent folder, use this for the scope:

Code:
strScope = "'Inbox', 'Sent Items'"

Microsoft recommends using single quotes to wrap folder names.

This DASL filter should find all flagged messages to or from the person:

Code:
strDASLFilter = "((NOT(" & Chr(34) & "urn:schemas:httpmail:messageflag" & Chr(34) & " IS NULL)) AND(" _ 
    & Chr(34) & "urn:schemas:httpmail:displaycc" & Chr(34) & " LIKE '%" _ 
    & strFilter & "%' OR " & Chr(34) & "urn:schemas:httpmail:fromname" & Chr(34) _ 
    & " LIKE '%" & strFilter & "%' OR " & Chr(34) & "urn:schemas:httpmail:displayto" & Chr(34) _ 
    & "  LIKE '%" & strFilter & "%'))"

I'd probably search for all (using the DASL filer in rst's example) and use a view to group by flag or hide unflagged messages.
 
Thank you both for this help. My requirement is slightly complicated.

I am trying to create a search folder for the logic given above which will search in all data stores.

I am trying to use olSearchScopeAllFolders but it's not working.

Can either help?
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
R writing "Instant Search" queries to find User-Defined fields Using Outlook 0
S Outlook 2007 Calendar instant search problem. Windows 7 Using Outlook 4
S Outlook 2007 Calendar instant search problem. Windows 7 Using Outlook 0
B Instant search with special characters Using Outlook 1
Diane Poremsky Rebuild Outlook's Instant Search Index Using Outlook 0
N "Instant Search" setting a default view Using Outlook 3
G Outlook 2007 compound (OR) instant search break down Using Outlook 14
B Outlook 2013 instant search fields greyed out Using Outlook 3
L Outlook 2010 Instant Search Pops When I Click Using Outlook 6
S Instant Search in Outlook 2010 Using Outlook 32
S Outlook 2010 Terminalserver Cache Mode Instant Search Using Outlook 4
M How to resize the instant search pane on click on the icon to expand/ Outlook VBA and Custom Forms 1
M How to provide instant search like feature in outlook 2003? Outlook VBA and Custom Forms 1
P Disable Instant Search in Outlook 2007 Outlook VBA and Custom Forms 9
M Search message, then (1) Jump to folder & (2) Select message that you searched for Outlook VBA and Custom Forms 2
G Search Folders and Jump to Folder Outlook VBA and Custom Forms 2
A 'search people' now asks me to 'press enter' Using Outlook 8
A Relocate Search Bar in Outlook Using Outlook 2
A Search folder and move the email Outlook VBA and Custom Forms 0
farrissf Outlook 2016 Optimizing Email Searches in Outlook 2016: Seeking Insights on Quick Search vs Advanced Search Features Using Outlook 0
C Advanced search terms for "Outlook Data File" Using Outlook 1
T How to find or display the sub-folder name for an Archive Search Using Outlook 10
P Search folder: all emails sent to or from a domain Using Outlook 1
T Outlook365 search item listed as "potential matches" can't be opened Using Outlook 0
Victor_50 Outlook 2019 Jump to folder from search folder Outlook VBA and Custom Forms 0
E Outlook 365 Save Selected Email Message as .msg File - oMail.Delete not working when SEARCH Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro GoTo user defined search folder Outlook VBA and Custom Forms 6
J Outlook search bar in Office 2021 Professional Using Outlook 1
T Outlook 365 Search Box reverting to old location Using Outlook 2
P Outlook 2013 search no longer works Using Outlook 2
C How to search for items in Outbox with multiple accounts? Using Outlook 20
J search doesn't find anything Using Outlook 1
kkqq1122 How would I add Search for attachment name Outlook VBA and Custom Forms 3
A Any way to force sort by/group by on search results with VBA? Outlook VBA and Custom Forms 1
B Search and Find Email by Folder Name Outlook VBA and Custom Forms 2
C Search not accurate, but not indexing Using Outlook 9
J How do you disable address search box when typing @ in body of email? Using Outlook 0
H Outlook 2019 intermittent search results Using Outlook 0
D Create advanced search (email) via VBA with LONG QUERY (>1024 char) Outlook VBA and Custom Forms 2
D Advanced e-Mail search on from/to contact group only searches for first 20 contacts in group Using Outlook 0
mll persistently customise columns in outlook advanced search Using Outlook 3
S vba outlook search string with special characters Outlook VBA and Custom Forms 1
S VBA search string with special characters Outlook VBA and Custom Forms 1
H Search Email Header for Content Type Outlook VBA and Custom Forms 1
P Posts in Folder No Longer Group by Conversation Column After Search Using Outlook 0
O Advanced search - can I use booleans Using Outlook 3
Y Filter unread emails in a search folder vba help Outlook VBA and Custom Forms 0
M Reverting The Outlook Search Box Location (or other undesired additions) Using Outlook 1
M Something went wrong and your search couldn't be completed Using Outlook 1
M Disable Contact Card Results when using "Search People" in Outlook Ribbon Using Outlook 7

Similar threads

Back
Top