After establishing a Session object and successfully logging on to the system, a user can access the Inbox. The Inbox is the default folder for mail received by the user.
As described in CDO Library Object Design, the CDO Library objects are organized in a hierarchy. The Session object at the topmost level allows access to a Folder object. Each folder contains a Messages collection, which contains individual Message objects. The text of the message appears in its Text property.
Session object
Folder object
Messages collection
Message object
Text property
To obtain an individual message, the application must move down through this object hierarchy to the Text property. The following example uses the Session object's Inbox property to obtain a Folder object, then uses the folder's Messages property to obtain a Messages collection object, and calls the collection's methods to get a specific message.
This code fragment assumes that the application has already created the Session object variable objSession and successfully called the Session object's Logon method, as described in Starting a CDO Session:
Dim objSession As MAPI.Session ' Session object
Dim objInboxFolder As Folder ' Folder object
Dim objInMessages As Messages ' Messages collection
Dim objOneMsg As Message ' Message object
' ...
' move down through the hierarchy
Set objInboxFolder = objSession.Inbox
Set objInMessages = objInboxFolder.Messages
Set objOneMsg = objInMessages.GetFirst
MsgBox "The message text: " & objOneMsg.Text
Note Use the Microsoft® Visual Basic® keyword Set whenever you initialize a variable that represents an object. When you attempt to set an object variable without using the Set keyword, Visual Basic generates an error message.
The preceding code fragment declares several object variables. However, it is also possible to access the message with fewer variables. The following code fragment is equivalent to the preceding code, and is preferable if you have no subsequent need for the Inbox folder or its Messages collection:
Set objOneMsg = objSession.Inbox.Messages.GetFirst
MsgBox "The message text: " & objOneMsg.Text
You should declare an individual variable when the application needs to access an object more than once. When an object is accessed repeatedly, variables can help make your code efficient. For more information, see Improving Application Performance.
Creating and Sending a Message, Searching for a Message