Copying a Message to Another Folder

Copying a Message to Another Folder

The procedure documented in this section first demonstrates the old way to copy message properties using the Messages collection’s Add method, and then demonstrates how to take advantage of the newer CopyTo method of the Message object.

Note    With versions of CDO Library previous to 1.1, the Message object’s Sender property and other read-only properties of the Message object were not preserved during the first part of the procedure in this section. To preserve these properties using the old procedure, you had to append their text fields to read/write properties, such as the Message object’s Text property.

With the CopyTo method, every property that is set on a Message object is automatically copied to the new Message object, regardless of whether it has read-only or read/write access. The access capability of every property is also preserved across the copy.

ms526910.787845a5-a0eb-4ef0-a02b-af44a9f76e33(en-us,EXCHG.10).gif

To copy a message from one folder to another folder using the CDO Library

  1. Obtain the source message that you want to copy.
  2. Call the destination folder’s Messages collection’s Add method, supplying the source message properties as parameters. – Or –
  3. Call the source Message object’s CopyTo method.
  4. Call the new Message object’s Update method to save all new information in the MAPI system.

The hierarchy of objects is as follows:

Session object     Folder object (Inbox or Outbox)         Messages collection             Message object     InfoStores collection         InfoStore object             Folder object                 Messages collection                     Message object

To obtain the source message that you want to copy, first obtain its folder, then obtain the message within the folder’s Messages collection. For more information about finding messages, see Searching for a Message.

To obtain the destination folder, you can use the following approaches:

  • Use the Folders collection’s Get methods to search for a specific folder.
  • Call the Session object’s GetFolder method with a string parameter that specifies the FolderID, a unique identifier for that folder.

For more information about finding folders, see Searching for a Folder.

The following example copies the first two messages in the given folder to the Inbox. They could as easily be copied to any folder with a known identifier and therefore accessible using the Session object’s GetFolder method. The example uses the old procedure to copy the first message and the new CopyTo method to copy the second.

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.

'/********************************/
' Function: Util_CopyMessage
' Purpose: Utility functions that demonstrates code to copy a message
' See documentation topic: Copying A Message To Another Folder
Function Util_CopyMessage()
' obtain the source messages to copy
' for this sample, just copy the first two messages to the Inbox
' assume session object already created, validated, and logged on
Dim objMsgColl As Messages   ' given folder's Messages collection
Dim objThisMsg As Message    ' original message from given folder
Dim objInbox As Folder       ' destination folder is Inbox
Dim objCopyMsg As Message    ' new message that is the copy
Dim objOneRecip As Recipient ' single message recipient being copied
Dim strRecipName As String   ' recipient name from original message
Dim i As Integer             ' loop counter

On Error GoTo error_olemsg
If objGivenFolder Is Nothing Then
    MsgBox "Must supply a valid folder"
    Exit Function
End If
Set objMsgColl = objGivenFolder.Messages ' to be reused later
' ( ... then validate the Messages collection before proceeding ... )
Set objThisMsg = objMsgColl.GetFirst() ' filter parameter not needed
If objThisMsg Is Nothing Then
    MsgBox "No valid messages in given folder"
    Exit Function
End If
' Get Inbox as destination folder
Set objInbox = objSession.Inbox
If objInbox Is Nothing Then
    MsgBox "Unable to open Inbox"
    Exit Function
Else
    MsgBox "Copying first message to Inbox"
End If
' Copy first message using old procedure
Set objCopyMsg = objInbox.Messages.Add _
   (Subject:=objThisMsg.Subject, _
    Text:=objThisMsg.Text, _
    Type:=objThisMsg.Type, _
    Importance:=objThisMsg.Importance)
If objCopyMsg Is Nothing Then
    MsgBox "Unable to create new message in Inbox"
    Exit Function
End If
' Copy all the recipients
For i = 1 To objThisMsg.Recipients.Count Step 1
    strRecipName = objThisMsg.Recipients.Item(i).Name
    If strRecipName <> "" Then
        Set objOneRecip = objCopyMsg.Recipients.Add
        If objOneRecip Is Nothing Then
            MsgBox "Unable to create recipient in message copy"
            Exit Function
        End If
        objOneRecip.Name = strRecipName
    End If
Next i
' Copy other properties; a few listed here as an example
objCopyMsg.Sent = objThisMsg.Sent
objCopyMsg.Text = objThisMsg.Text
objCopyMsg.Unread = objThisMsg.Unread
' Update new message so all changes are saved in MAPI system
objCopyMsg.Update
' If MOVING a message to another folder, delete the original message:
'     objThisMsg.Delete
' Move operation implies that the original message is removed

' Now copy second message using new procedure
Set objThisMsg = objMsgColl.GetNext ()
' ( ... then validate the second message before proceeding ... )
Set objCopyMsg = objThisMsg.CopyTo (objInbox.ID)
' Then Update and we're done
objCopyMsg.Update
Exit Function

error_olemsg:
MsgBox "Error " & Str(Err) & ": " & Error$(Err)
Exit Function ' so many steps to succeed; just exit on error

End Function
 

Note that the old procedure does not preserve all message properties. The CopyTo method copies all properties with their values and access capabilities (read-only or read/write) unchanged.

See Also

Moving a Message to Another Folder

See Also

Concepts

Programming Tasks