Filters

To assist with locating specific items in Messages and AddressEntries collections, CDO provides the MessageFilter and AddressEntryFilter objects. Every Messages collection has a MessageFilter object associated with it, which can be accessed through the Messages collection’s Filter property. Similarly, every AddressEntries collection has an AddressEntryFilter object associated with it, accessible through the AddressEntries collection’s Filter property. By setting properties on a filter object, the associated collection retrieves only those items that fulfill the criteria thus specified. This is far more efficient than using code to read through the entire collection, examine each item to determine if it meets the criteria, and act on only the items of interest. The code in Example 7-9 processes only unread messages having a message class of "IPM.MyMessageClass“.

Example 7-9. Constraining a Messages Collection

' CdoSession previously Dim'ed and Set. Dim CdoFolder As MAPI.Folder Dim CdoMessages As MAPI.Messages Dim CdoMessageFilter As MAPI.MessageFilter Dim CdoMessage As MAPI.Message Set CdoFolder = CdoSession.Inbox Set CdoMessages = CdoFolder.Messages Set CdoMessageFilter = CdoMessages.Filter ' Constrain the Messages collection. CdoMessageFilter.Type = "IPM.MyMessageClass" CdoMessageFilter.Unread = True For Each CdoMessage In CdoMessages ' Do something with the message. Next CdoMessage ' Clean up. Set CdoMessage = Nothing Set CdoMessageFilter = Nothing Set CdoMessages ...

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.