Share via


Programming Tasks

Programming Tasks

This section describes some of the common programming tasks you can perform with the Collaboration Data Objects (CDO) Library. The first task your application must complete is to obtain and Logon to a valid Session object as described in Starting a CDO Session. The following table categorizes the described tasks:

Category

Programming tasks

General programming tasks

Handling Errors Improving Application Performance Starting a CDO Session Viewing MAPI Properties

Working with messages

Adding Attachments to a Message Checking for New Mail Creating and Sending a Message Customizing a Folder or Message Deleting a Message Filtering Messages in a Folder Making Sure the Message Gets There Reading a Message from the Inbox Searching for a Message

Working with addresses

Changing an Existing Address Entry Creating a New Address Book Entry Selecting Recipients from the Address Book Using Addresses Working with Distribution Lists

Working with folders

Accessing Folders Copying a Message to Another Folder Customizing a Folder or Message Moving a Message to Another Folder Searching for a Folder

Working with public folders

Posting Messages to a Public Folder Working with Conversations

The following table summarizes the programming procedures that you must use to perform these tasks. Note that all tasks require a valid

Session object and a successful Logon.Programming task

Procedure

Accessing Folders

1. Access the Folder object’s Folders property to obtain its collection of subfolders.

2. Use the Folders collection’s GetFirst, GetNext, GetPrevious, and GetLast methods to navigate through the subfolders.

Adding Attachments to a Message

1. Create or obtain the Message object that is to include the attachment.

2. Call the Message object’s Attachments collection’s Add method.

3. Call the Message object’s Update or Send method.

Changing an Existing Address Entry

1. Obtain a valid AddressEntry object.

2. Update the AddressEntry object’s Name, Type, or Address property.

3. Call the AddressEntry object’s Update method.

Checking for New Mail

Count messages in the Inbox folder that have the Unread property set to True.

– Or –

Count messages received after a specified time.

Copying a Message to Another Folder

1. Obtain the source message that you want to copy.

2. Call the source Message object’s CopyTo method.

3. Call the new Message object’s Update method.

Creating a New Address Book Entry

1. Obtain the Session object’s AddressLists collection.

2. Select the AddressList object corresponding to the desired address book container.

3. Obtain the address list’s AddressEntries collection.

4. Call the AddressEntries collection’s Add method.

Creating and Sending a Message

1. Call the Messages collection’s Add method to create a Message object.

2. Set the Message object’s Text, Subject, and other message properties.

3. Call the message’s Recipients collection’s Add method to add a recipient.

– Or –

3. Copy a Recipients collection from another message to the new message’s Recipients property.

4. Set the Recipient object’s Name, Address,or AddressEntry property.

5. Call the Recipient object’s Resolve method to validate the address information.

6. Call the Message object’s Send method.

Customizing a Folder or Message

1. Create or obtain the Folder or Message object that is to have the custom properties.

2. Call the object’s Fields collection’s Add method.

Deleting a Message

1. Select the message you want to delete.

2. Call the Message object’s Delete method.

Filtering Messages in a Folder

1. Access the folder in which you wish to filter the messages.

2. Obtain the MessageFilter object for the folder.

3. Select and set the desired MessageFilter properties to specify the filter.

Handling Errors

Use the Microsoft® Visual Basic® On Error Goto statement to add exception-handling code just as you would in any Visual Basic application.

Improving Application Performance

Each dot in a Visual Basic statement directs the CDO Library to create a temporary internal object. Use explicit variables when you reuse messaging objects.

Making Sure the Message Gets There

1. Set the Message object’s DeliveryReceipt and/or ReadReceipt properties to True.

2. Call the Message object’s Send method.

Moving a Message to Another Folder

1. Obtain the source message that you want to move.

2. Call the source Message object’s MoveTo method.

3. Call the Message object’s Update method at its new location.

Organizing a Meeting

1. Obtain a calendar folder from the session.

2. Add an appointment to the calendar folder.

3. Create a Recipients collection for the appointment.

4. Populate the appointment's Recipients collection.

5. Send the appointment to the recipients.

Posting Messages to a Public Folder 

Use a procedure similar toCreating and Sending a Message, where you specify the name of the public folder as the recipient name.

– Or –

1. Call the public folder’s Messages collection’s Add method to create a Message object.

2. Set the Message object’s Text, Subject, ConversationSubject, ConversationIndex, TimeSent, TimeReceived,and other message properties.

3. Set the Message object’s Unread, Submitted, and Sent properties to True.

4. Call the Message object’s Send or Update method to post the message.

Reading a Message from the Inbox

1. Call the session’s Inbox folder’s GetFirst, GetNext, GetPrevious, and GetLast methods to obtain a Message object.

2. Obtain the Message object’s Text property.

Searching for a Folder

Use the Session object’s GetFolder method to obtain the folder from its known identifier value.

– Or –

Call the Folders collection’s Get methods to get individual Folder objects, and compare properties of each folder with the desired property values.

Searching for a Message

Use the Session object’s GetMessage method to obtain the message from its known identifier value.

– Or –

Call the Messages collection’s Get methods to get individual Message objects, using a message filter to reduce the number of messages searched, and if necessary compare properties of each message with the desired property values.

Selecting Recipients from the Address Book

1. Call the session’s AddressBook method to use the MAPI address book dialog box.

2. Set a Recipients collection object to the Recipients collection returned by the AddressBook method.

3. Use that Recipients collection or copy individual recipients from it.

Starting a CDO Session

1. Create or obtain a Session object.

2. Call the Session object’s Logon method.

Using Addresses

1. Set the message’s Recipient object’s Address property to a full address.

2. Call the Recipient object’s Resolve method.

Viewing MAPI Properties

Specify a MAPI property tag as the Fields collection’s Item property.

Working with Conversations

1. Set the message’s ConversationTopic property.

2. Set the message’s ConversationIndex property.

3. Send the message by calling the Send method.

– Or –

3. Post the message in the public folder by setting the Submitted property to True.

Working with Distribution Lists

1. Add a distribution list (DL) to a personal address book (PAB).

2. Add an address entry to a distribution list.

3. Delete an address entry from a distribution list.

It is important to understand the hierarchy of the CDO Library objects, because the hierarchical relationships between objects determines the correct syntax of Microsoft® Visual Basic® statements. The relative positions of these objects in the hierarchy indicate how the objects appear from left to right in a Visual Basic statement. For more information on the hierarchy, see Object Model.

In the sample code that appears in this guide, individual statements are often broken across several lines. The convention used for this is the statement continuation introduced in Visual Basic version 4.0, which consists of a space followed by the underscore character (_). This sequence is placed at the end of a code line to indicate that the current statement is continued on the next line.