AdvancedSearch Method

Microsoft Outlook Visual Basic

AdvancedSearch Method

       

Performs a search based on a specified SQL search string and returns a Search object.

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.

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. In order to capture meaningful results, however, use the AdvancedSearchComplete event to signal the end of a synchronous search.

Example

The following 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 aren't flagged .

Sub SearchForFlags()
    'List all items in the Inbox that do NOT have a flag:
    Dim objSch As Search
    Const strF As String = "urn:schemas:httpmail:messageflag = 0" & _
        " OR urn:schemas:httpmail:messageflag IS NULL"
    Const strS As String = "Inbox"
    Set objSch = Application.AdvancedSearch(strS, strF)
End Sub

The following example returns all items in the Inbox with the subject Office Christmas Party. The Tag argument associates the search with a string. This is useful when conducting multiple searches.

Sub SearchInboxFolder()
'Searches the Inbox
    Dim objSch As Search
    Const strF As String = "urn:schemas:mailheader:subject = 'Office Christmas Party'"
    Const strS As String = "Inbox"
    Const strTag As String = "SubjectSearch"
    Set objSch = Application.AdvancedSearch(Scope:=strS, _
        Filter:=strF, Tag:=strTag)
End Sub

The following example uses the AdvancedSearchComplete event to capture the outcome of the search. The tag property of the search is displayed to notify the user which search has completed. This event is important because it signals the end of the search and allows you to trap a complete and meaningful set of data. This is the only way to ensure that the search has completed.

Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)
    Dim objRsts As Results
    MsgBox "The search " & SearchObject.Tag & "has completed."
    Set objRsts = SearchObject.Results
    'Print out number in Results collection
    Debug.Print objRsts.Count
    'Print out each member of Results collection
    For Each Item In objRsts
        Debug.Print Item
    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.

Sub SearchForSubject()
'Search for all items with a certain subject
'in multiple folders

    Dim objSch As 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