Platform SDK: CDO 1.2.1 |
To access a message, you can search for it using one of the following techniques:
When you know the unique identifier for the message you are looking for, you can call the Session object’s GetMessage method.
The message identifier specifies a unique identifier that is created for the Message object at the time it is created. The identifier is accessible through the Message object’s ID property.
The following code fragment contains code that saves the identifier for the message, then uses it in a subsequent GetMessage call:
' Function: Session_GetMessage ' Purpose: Demonstrate how to set a message object using GetMessage ' See documentation topic: GetMessage method (Session object) Function Session_GetMessage() On Error GoTo error_olemsg If objSession Is Nothing Then MsgBox "No active session, must log on" Exit Function End If If strMessageID = "" Then MsgBox ("Must first set Message ID variable; see Message->ID") Exit Function End If Set objOneMsg = objSession.GetMessage(strMessageID) If objOneMsg Is Nothing Then MsgBox "Unable to retrieve message with specified ID" Exit Function End If MsgBox "GetMessage returned msg with subject: " & objOneMsg.Subject Exit Function error_olemsg: MsgBox "Error " & Str(Err) & ": " & Error$(Err) Set objOneMsg = Nothing MsgBox "Message is no longer available" Exit Function End Function
When you are looking for a message within a Messages collection, you can navigate through the collection, examining properties of each Message object to determine if it is the message you want.
The CDO Library supports the GetFirst, GetNext, GetLast, and GetPrevious methods for the Messages collection object. You can also use the Visual Basic For Each construction to traverse the collection.
Note that, with the CDO Library version 1.1 and later, you can use a MessageFilter object to restrict a search with the Get methods. Obtain the message filter through the Messages collection’s Filter property, set the filter’s properties to the values desired for the search, and then proceed with the Get methods. Only the messages passing the filter criteria are returned for your inspection. For more information on message filtering, see Filtering Messages in a Folder.
The following sample demonstrates how to use the Get methods to search for the specified message:
' Function: TestDrv_Util_GetMessageByName ' Purpose: Call the utility function Util_GetMessageByName ' See documentation topic: Item property (Message object) Function TestDrv_Util_GetMessageByName() Dim fFound As Boolean On Error GoTo error_olemsg fFound = Util_GetMessageByName("Junk mail") If fFound Then MsgBox "Message named 'Junk mail' found" Else MsgBox "Message named 'Junk mail' not found" End If Exit Function error_olemsg: MsgBox "Error " & Str(Err) & ": " & Error$(Err) Resume Next End Function ' Function: Util_GetMessageByName ' Purpose: Use Get* methods to search for a message ' See documentation topic: Searching for a message ' Search through the messages for one with a specific subject Function Util_GetMessageByName(strSearchName As String) As Boolean Dim objOneMessage As Message ' local; temp version of message object On Error GoTo error_olemsg Util_GetMessageByName = False ' default; assume failure If objFolder Is Nothing Then MsgBox "Must first select a folder such as Session->Inbox" Exit Function End If Set objMessages = objFolder.Messages Set objOneMessage = objMessages.GetFirst If objOneMessage Is Nothing Then MsgBox "No messages in the folder" Exit Function End If ' loop through all the messages in the collection Do While Not objOneMessage Is Nothing If objOneMessage.Subject = strSearchName Then Exit Do ' found it, leave the loop Else ' keep searching Set objOneMessage = objMessages.GetNext End If Loop ' exit from the Do While loop comes here ' if objOneMessage is valid, the message was found If Not objOneMessage Is Nothing Then Util_GetMessageByName = True ' success End If Exit Function error_olemsg: MsgBox "Error " & Str(Err) & ": " & Error$(Err) Resume Next End Function