How To Programmatically Copy a Message Type Attachment to a Folder (231958)



The information in this article applies to:

  • Collaboration Data Objects (CDO) 1.2
  • Collaboration Data Objects (CDO) 1.21

This article was previously published under Q231958

SUMMARY

You can copy an attachment from a message (of type CdoEmbeddedMessage) to a folder by using your right mouse button to drag and drop. This article describes how you can do the same thing programmatically using the CDO (1.2, 1.21) Library.

MORE INFORMATION

The following example goes through all the messages in the Inbox folder, finds the first message of type attachment, and copies the attachment to the Inbox.
  1. Create a Visual Basic application.
  2. Reference the CDO (1.2, 1.21) library.
  3. Add a button to the form and name it Command1.
  4. Paste following code into the application:
    Private Sub Command1_Click()
        Dim objSession As MAPI.Session
        Dim objInbox As Folder
        Dim objMessages As Messages
        Dim objMessage As Message
        Dim objEmbeddedMessage As Message
        Dim objNewMail As Message
        
        'create session and logon
        Set objSession = CreateObject("MAPI.Session")
        objSession.Logon
        
        'reference the messages in the inbox
        Set objInbox = objSession.Inbox
        Set objMessages = objInbox.Messages
        
        'find the first message in the inbox which has a message type attachment and copy it to inbox
        For Each objMessage In objMessages
            If objMessage.Attachments.Count > 0 Then
                If objMessage.Attachments(1).Type = CdoEmbeddedMessage Then
                    Set objEmbeddedMessage = objMessage.Attachments(1).Source
                    Set objNewMail = objEmbeddedMessage.CopyTo(objInbox.ID)
                    objNewMail.Update
                    Set objNewMail = Nothing
                    Set objEmbeddedMessage = Nothing
                End If
            End If
            
        Next
        
        objSession.Logoff
        Set objMessage = Nothing
        Set objMessages = Nothing
        Set objInbox = Nothing
        Set objSession = Nothing
    End Sub
    					
  5. Run the application.

REFERENCES

For more information, about the Source property of the Attachment Object, please see the CDO (1.2, 1.21) Help file (CDO.hlp).

Modification Type:MinorLast Reviewed:7/2/2004
Keywords:kbhowto kbMsg KB231958