Microsoft KB Archive/326244

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

Article ID: 326244

Article Last Modified on 4/8/2004

-

APPLIES TO


 * Microsoft Office Outlook 2003
 * Microsoft Outlook 2002 Standard Edition

-



This article was previously published under Q326244



IN THIS TASK

 * INTRODUCTION
 * Information about the AdvancedSearch method
 * Information about the AdvancedSearchComplete event
 * REFERENCES



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 = &quot;Inbox&quot; strT = InputBox(&quot;Enter the subject text.&quot;, &quot;Search Criteria&quot;) strF = &quot;urn:schemas:httpmail:subject LIKE '%&quot; & strT & &quot;'&quot; strTag = &quot;SubjectSearch&quot; ' 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 = &quot;Inbox&quot; strF = &quot;urn:schemas:httpmail:importance = 2&quot; strTag = &quot;ImportanceSearch&quot; 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 (&quot;'\PSTFolder\PST SubFolder'&quot;, 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 &quot;\PSTFolder\PST SubFolder&quot; to the PST's FolderPath ' property while omitting the first \ strS = &quot;'\PSTFolder\PST SubFolder'&quot; strF = &quot;urn:schemas:httpmail:importance = 2&quot; strTag = &quot;ImportanceSearch&quot; 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 object Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search) Dim objResults As Results Set objResults = SearchObject.Results MsgBox &quot;AdvancedSearch found : &quot; & objResults.count For Each objResult In objResults Debug.Print objResult.Subject Next End Sub back to the top

