Filtering items in a collection

Microsoft Office Outlook 2003

You can use the Microsoft Outlook object model to return information about all items in a folder. Often, however, the desired objective is to search for a specific item or to retrieve a subset of the items in the folder. Consider the following examples:

  • You are developing a Microsoft Access database. When the user enters a new contact record, you want to give the user the ability to click a button to check whether a contact with the same name already exists in Outlook. If a match is found, you can retrieve all the fields for the contact and automatically fill in the Access database record. In this situation, if the user filled in the first and last name fields on the Access form, you can use the Find method in the Outlook object model to search for a match against the Outlook Full Name field. If you want to make sure there are no additional contacts in Outlook with the same name, you can then use the FindNext method to conduct the same search again.
  • You are writing a Microsoft Visual Basic program to automatically schedule appointments in users’ calendars. In order to do this, you need to retrieve a user’s appointments for a given day. In this case, you would use the Restrict method to retrieve all appointments that fall on a particular day.

While the Find and Restrict methods perform different functions, the syntax for both is similar. Following are some tips for using these methods:

  • The filters used with Find and Restrict are not case-sensitive.
  • It is not possible to use these methods if you need to search for a string that’s contained within an Outlook field, commonly called a "Contains" operation. If you need to perform a Contains operation, you can iterate through all of the items in the folder and use the Visual Basic InStr function to see if your search string is contained within an Outlook field.
  • To create a filter that performs a "begins with" operation, use the >, < , >=, and <= operators. For example, to search for all contacts whose last names begin with "Mc", use this filter: ""[LastName] >= ""Mc"" and [LastName] < ""Md"""
  • You can use the contents of a variable as part of the filter.