You are currently offline, waiting for your internet to reconnect

How to use the AdvancedSearch method to search for an item in Outlook

This article was previously published under Q326244
INTRODUCTION
The Microsoft Outlook Object Model in Microsoft Outlook 2002 provides a programmatic approach that you can use to search Outlook items by using the AdvancedSearch method. This method performs an SQL-like syntax search and has four parameters that permit you to customize the search. The AdvancedSearchComplete event fires because of the AdvancedSearch method, and you can access the results collection in the AdvancedSearchComplete event handler.

back to the top

Information about the AdvancedSearch method

The AdvancedSearch method performs a string-specified search that is based on an SQL-like syntax search and returns a Search object. This method has the following four parameters that you can use to customize your Outlook search:
  • Scope: The Scope parameter specifies the name of the folder that is to be searched.
  • Filter: The Filter parameter is an optional string parameter that sets the search constraints based on the internal schema of the mail server.
  • SearchSubFolders: The SearchSubFolders parameter is an optional parameter that specifies whether to include the subfolders of the folder that is specified by the Scope parameter. This parameter is only valid for searches that are performed against a computer that is running Microsoft Exchange Server or against a PST folder.
  • Tag: The Tag parameter is an optional parameter that is used to name the search.
The following sample code uses the AdvancedSearch method to create a new search that returns all the items in the Inbox that have a user-specified subject:
Sub SearchInboxFolder()  Dim objSch As Search  Dim strF As String  Dim strS As String  Dim strT As String  Dim strTag As String      strS = "Inbox"  strT = InputBox("Enter the subject text.", "Search Criteria")  strF = "urn:schemas:httpmail:subject LIKE '%" & strT & "'"  strTag = "SubjectSearch"  ' TODO: Replace Application with the declared and set   ' Outlook Application object  Set objSch = Application.AdvancedSearch(strS, strF, False, strTag)End Sub
The following sample code uses the AdvancedSearch method to create a new search that returns all the items in the Inbox that are marked with high importance:
Sub SearchInboxFolder()  Dim objSch As Search  Dim strF As String  Dim strS As String  Dim strTag As String      strS = "Inbox"  strF = "urn:schemas:httpmail:importance = 2"  strTag = "ImportanceSearch"  Set objSch = Application.AdvancedSearch(strS, strF, False, strTag)End Sub
The following sample code uses the AdvancedSearch method to create a new search that returns all the items in a PST folder that are marked with high importance. If the FolderPath property of the folder contains spaces, use double quotes and single quotes when you set the search filter ("'\PSTFolder\PST SubFolder'", for example). If the FolderPath property of the folder does not contain spaces, you can omit the single quotes.
Sub SearchPSTFolder()  Dim objSch As Search  Dim strF As String  Dim strS As String  Dim strTag As String      ' TODO: Change "\PSTFolder\PST SubFolder" to the PST's FolderPath   ' property while omitting the first \   strS = "'\PSTFolder\PST SubFolder'"  strF = "urn:schemas:httpmail:importance = 2"  strTag = "ImportanceSearch"  Set objSch = objOL.AdvancedSearch(strS, strF, False, strTag)End Sub
back to the top

Information about the AdvancedSearchComplete event

The AdvancedSearchComplete event of the Outlook Application object is used to capture the outcome of a search that uses the AdvancedSearch method. The AdvancedSearchComplete event occurs when the AdvancedSearch method has finished running. This event returns the search object that was created in the AdvancedSearch method, and it guarantees a complete set. In the AdvancedSearchComplete event, additional code is used to handle and to interpret the data that is returned from the search.

This method has one parameter to connect this event to the AdvancedSearch method. The SearchObject parameter is a required parameter that specifies the search object that is returned by the AdvancedSearch method.

The following sample code displays the number of results that are returned by the AdvancedSearch method in a message box, and then prints the subject of each message in the debug window:
' TODO: Replace Application with the declared and set ' Outlook Application objectPrivate Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)  Dim objResults As Results  Set objResults = SearchObject.Results  MsgBox "AdvancedSearch found : " & objResults.count   For Each objResult In objResults    Debug.Print objResult.Subject  NextEnd Sub
back to the top
Properties

Article ID: 326244 - Last Review: 04/08/2004 17:16:04 - Revision: 1.4

  • Microsoft Office Outlook 2003
  • Microsoft Outlook 2002 Standard Edition
  • kboutlookobj kbhowtomaster kbinfo KB326244
Feedback