expression.AdvancedSearch(Scope, Filter, SearchSubFolders, Tag)
expression Required. An expression that returns an Application object.
Scope Required String. The scope of the search. For example, the name of a folder. It is recommended that the folder name is enclosed within single quotes. Otherwise, the search might not return correct results if the folder name contains special characters including Unicode characters.
Filter Optional Variant. The DASL search filter that defines the parameters of the search.
SearchSubFolders Optional Variant. Determines if the search will include any of the folder's subfolders.
Tag Optional Variant. The name given as an identifier for the search.
Remarks
You can run multiple searches simultaneously by calling the AdvancedSearch method in successive lines of code. A maximum of 100 simultaneous searches can be performed by using the Microsoft Outlook user interface and the Outlook object model.
The AdvancedSearch method and related features in the Outlook object model do not create a Search Folder that will appear in the Outlook user interface. However, you can use the Save method of the Search object that is returned to create a Search Folder that will appear in the Search Folders list in the Outlook user interface.
Example
The following Visual Basic for Applications (VBA) example searches the Inbox for items with subject equal to Test and displays the names of the senders of the e-mail items returned by the search. The AdvanceSearchComplete
event procedure sets the boolean blnSearchComp
to True when the the search is complete. This boolean variable is used by the TestAdvancedSearchComplete()
procedure to determine when the search is complete. The sample code must be placed in a class module such as ThisOutlookSession, and the TestAdvancedSearchComplete()
procedure must be called before the event procedure can be called by Outlook.
Public blnSearchComp As Boolean
Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)
MsgBox "The AdvancedSearchComplete Event fired"
blnSearchComp = True
End Sub
Sub TestAdvancedSearchComplete()
Dim sch As Outlook.Search
Dim rsts As Outlook.Results
Dim i As Integer
blnSearchComp = False
Const strF As String = "urn:schemas:mailheader:subject = 'Test'"
Const strS As String = "Inbox"
Set sch = Application.AdvancedSearch(strS, strF)
While blnSearchComp = False
DoEvents
Wend
Set rsts = sch.Results
For i = 1 To rsts.Count
MsgBox rsts.Item(i).SenderName
Next
End Sub
The following Microsoft Visual Basic/Visual Basic for Applications example uses the AdvancedSearch method to create a new search. The parameters of the search, as specified by the Filter argument of the AdvancedSearch method, will return all items in the Inbox that have the Subject as 'Test'. The user's Inbox is specified as the scope of the search and the SearchSubFolders property is set to True. The event subroutine occurs when the search has completed and displays the Tag and Scope properties for the new object as well as the results of the search.
Public blnSearchComp As Boolean
Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)
MsgBox "The AdvancedSearchComplete Event fired for " & SearchObject.Tag & " and the scope was " & SearchObject.Scope
blnSearchComp = True
End Sub
Sub TestAdvancedSearchComplete()
Dim objSch As Outlook.Search
Dim rsts As Outlook.Results
Dim i As Integer
blnSearchComp = False
Const strF1 As String = "urn:schemas:mailheader:subject = 'Test'"
Const strS1 As String = "Inbox"
Set objSch = _
Application.AdvancedSearch(Scope:=strS1, Filter:=strF1, SearchSubFolders:=True, Tag:="SubjectSearch")
While blnSearchComp = False
DoEvents
Wend
Set rsts = objSch.Results
For i = 1 To rsts.Count
MsgBox rsts.Item(i).SenderName
Next
End Sub
You can also conduct searches on multiple folders simultaneously. The following example searches for all items with the subject "Fiftieth Birthday Party" in the user's Inbox, Calendar, and Tasks folders. Note that you need to use the AdvancedSearchComplete event to determine when the search is complete and work on the results.
Sub SearchForSubject()
'Search for all items with a certain subject
'in multiple folders
Dim objSch As Outlook.Search
'Search for items where subject is not an empty string
Const strFilter As String = _
"urn:schemas:httpmail:subject = 'Fiftieth Birthday Party'"
'In the Inbox, Calendar, and Tasks folders
Const strScope As String = "'Inbox', 'Calendar', 'Tasks'"
Set objSch = Application.AdvancedSearch(strScope, strFilter)
End Sub
The following is another example that uses the AdvancedSearch method.
Public sch As Outlook.Search
Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)
Dim rsts As Outlook.Results
If (SearchObject.Tag = "Search1") Then
Set rsts = sch.Results
MsgBox "Search1 returned " & rsts.Count & " items"
End If
End Sub
Sub TestAdvancedSearchComplete()
Dim rsts As Outlook.Results
Dim i As Integer
Const strF As String = "urn:schemas:mailheader:subject = 'Thnx'"
Const strS As String = "Inbox"
Set sch = Application.AdvancedSearch(strS, strF, , "Search1")
End Sub