Microsoft KB Archive/231958

= How To Programmatically Copy a Message Type Attachment to a Folder =

Article ID: 231958

Article Last Modified on 7/2/2004

-

APPLIES TO


 * Microsoft Collaboration Data Objects 1.2
 * Microsoft Collaboration Data Objects 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.  Create a Visual Basic application. Reference the CDO (1.2, 1.21) library. Add a button to the form and name it Command1.  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  Run the application.

