Filtering Contacts

By default, a contacts folder object’s Messages collection contains a Message object for each contact in the folder. To limit the contacts that are returned to the client, use the MessageFilter object returned by the Filter property of the Messages collection. MessageFilter objects were introduced in Chapter 7, and they are used here in the same way. In addition to the standard filtering that can be achieved by using the MessageFilter object’s properties, it is also possible to filter on contact-specific properties through the use of the MessageFilter object’s Fields collection. The general steps are:

  1. Obtain a reference to the Messages collection of a contacts folder.

  2. Obtain a reference to the MessageFilter object returned by the Filter property of the Messages collection.

  3. Obtain a reference to the MessageFilter object’s Fields collection.

  4. Using the Fields collection’s Add method, add a Field object that represents the specific property on which to filter.

  5. Set the Value property of the Field object to the desired value.

  6. If desired, add additional Field objects and set their Value properties.

  7. Iterate through the Message objects in the Messages collection obtained in Step 1. Only Message objects that match the filter criteria will be seen.

These steps are illustrated in Example 10-5, which shows a segment of code that filters the Contacts folder for contacts having an address in Honolulu, Hawaii, USA. Notice the use of the LetMAPIProperty subroutine from Example 10-4 ...

Get CDO & MAPI Programming with Visual Basic: now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.