HOWTO: Programmatically Copy a Message Type Attachment to a Folder

ID: Q231958


The information in this article applies to:


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 articles describes how you can do the same thing programmatically.


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 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 Help file (CDO.hlp).

Additional query words: kbCDO kbCDO120 kbCDO121 kbDSupport kbGrpMsg kbMsg kbVb


Keywords          : kbCDO kbCDO120 kbCDO121 kbMsg kbGrpMsg kbDSupport 
Version           : WINDOWS:1.2,1.21
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: June 9, 1999