Delete Method (CDONTS Message Object)

The Delete method removes the Message object from the Messages collection.

Syntax

objMessage.Delete( ) 
 

Remarks

The Delete method performs an irreversible operation on the collection. It calls Release on the collection's reference to the Message object. If you have another reference to the message, you can still access its properties and methods, but you can never again associate it with any collection because the Add method always creates a new object. You should Set your reference variable either to Nothing or to another message.

The final Release on the Message object takes place when you call Delete if you had no other reference, or when you assign your reference variable to Nothing. At this point the object is removed from memory. Attempted access to a released object results in an error return of CdoE_INVALID_OBJECT.

The action of the Delete method is permanent, and the Message object cannot be restored to the collection. Before calling Delete, your application can prompt the user to verify whether the message should be permanently deleted.

You can delete all the messages in the Messages collection by calling the collection's Delete method. The ability to delete any message depends on the permissions granted to the user. The Delete method returns an error code if called with insufficient permissions.

The CDO for NTS Library does not permit any modifications to messages in the Inbox, other than deleting the entire message. Prohibited modifications include adding, deleting, or modifying any attachment; adding, deleting, or modifying any recipient; and modifying any message property, even one with read/write access.

Example

This code fragment illustrates the two situations previously explained. The Set statement calls AddRef on the first Message object. That reference survives the call to Delete and has to be reassigned. The second Message object is deleted without creating another reference, and no other action is necessary.

' assume valid Folder object (only Inbox meaningful in this context) 
Set objMessage = objFolder.Messages.Item(1) 
objMessage.Delete ' still have a reference from Set statement 
' ... other operations on objMessage possible but pointless ... 
Set objMessage = Nothing ' necessary to remove reference 
' ... 
objFolder.Messages.Item(2).Delete ' no reference to remove