Platform SDK: CDO 1.2.1 |
You can add one or more attachments to a message. You add each attachment to the Attachments collection obtained from the Message object’s Attachments property. The relationship between the Message object and an attachment is shown here:
Message object
Attachments collection
Attachment object
Type property
Source property
The CDO Library supports several different kinds of attachments: files, links to files, OLE objects, and embedded messages. An attachment’s type is specified by its Type property. To add an attachment, use the related Attachment object property or method appropriate for that type, as shown in the following table:
Attachment type | Related Attachment object property or method |
---|---|
CdoFileData | ReadFromFile method |
CdoFileLink | Source property |
CdoOLE | ReadFromFile method |
CdoEmbeddedMessage | ID property of the Message object to be embedded |
The following example demonstrates inserting a file as an attachment. This example 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: Attachments_Add_Data ' Purpose: Demonstrate the Add method for type = CdoFileData ' See documentation topic: Adding Attachments To A Message, ' Add method (Attachments collection) Function Attachments_Add_Data() Dim objMessage As Message ' local Dim objRecip As Recipient ' local On Error GoTo error_olemsg If objSession Is Nothing Then MsgBox ("must first log on; use Session->Logon") Exit Function End If Set objMessage = objSession.Outbox.Messages.Add If objMessage Is Nothing Then MsgBox "could not create a new message in the Outbox" Exit Function End If With objMessage ' message object .Subject = "attachment test" .Text = "Have a nice day." .Text = " " & objMessage.Text ' add placeholder for attachment Set objAttach = .Attachments.Add ' add the attachment If objAttach Is Nothing Then MsgBox "Unable to create new Attachment object" Exit Function End If With objAttach .Type = CdoFileData .Position = 0 ' render at first character of message .Name = "c:\smiley.bmp" .ReadFromFile "c:\smiley.bmp" End With objAttach.Name = "smiley.bmp" .Update ' update message to save attachment in MAPI system End With MsgBox "Created message, added 1 CdoFileData attachment, updated" Exit Function error_olemsg: MsgBox "Error " & Str(Err) & ": " & Error$(Err) Resume Next End Function
The attachment overwrites the placeholder character at the rendering position specified by the attachment’s Position property. A space is normally used for the placeholder character.
The CDO Library does not actually place the attachment within the message; that is the responsibility of the messaging client application. You can also use the value –1 for the Position property, which indicates that the attachment should be sent with the message, but should not be rendered by the Position property.
To insert an attachment of type CdoOLE, use code similar to the CdoFileData type example. Set the attachment type to CdoOLE and make sure that the specified file is a valid OLE docfile (a file saved by an OLE-aware application such as Microsoft® Word version 7.0 that uses the OLE interfaces IStorage and IStream).
To add an attachment of type CdoFileLink, set the Type property to CdoFileLink and set the Source property to the file name. The following sample code demonstrates this type of attachment:
' Function: Attachments_Add ' Purpose: Demonstrate the Add method for type = CdoFileLink ' See documentation topic: Adding Attachments To A Message, ' Add method (Attachments collection) Function Attachments_Add() On Error GoTo error_olemsg If objAttachColl Is Nothing Then MsgBox "must first select an attachments collection" Exit Function End If Set objAttach = objAttachColl.Add ' add the attachment With objAttach .Type = CdoFileLink .Position = 0 ' render at first character of message .Source = "\\server\bitmaps\honey.bmp" End With ' must update the message to save the new info objOneMsg.Update ' update message; save attachment in MAPI system MsgBox "Added an attachment of type CdoFileLink" Exit Function error_olemsg: MsgBox "Error " & Str(Err) & ": " & Error$(Err) Resume Next End Function