Sending File Attachments

MAPI defines four types of attachments: file, reference to a file, OLE object, and embedded message.

CDO handles all four types of attachments easily. To add an attachment to a message, use the Add method of the Message object’s Attachments collection:

' (CdoMessage previously Dim'ed and Set)

Dim CdoAttachments As MAPI.Attachments
Dim CdoAttachment As MAPI.Attachment

Set CdoAttachments = CdoMessage.Attachments
Set CdoAttachment = CdoAttachments.Add(Name:="autoexec.bat", _
   Position:=-1, Type:=CdoFileData, Source:="c:\autoexec.bat")

The syntax of the Add method is:

Set CdoAttachment = CdoAttachments.Add([Name][, Position][, Type][, Source])

The method returns a reference to the newly created Attachment object. The method’s parameters are:

Name

The display name to be used by the receiving mail client when representing the attachment in its UI. When the attachment is a file, the name should be the filename, including its extension. The receiving mail client may default to this name when copying the data from its message store to the local file system. (Some clients may instead default to the filename portion of the Source parameter.)

Position

The character position within the body of the message at which the attachment should appear. Some email client applications display a representation of the attachment within the body of the message at the character position indicated by Position. (However, some ignore this value and display the attachments separately.) ...

Get CDO & MAPI Programming with Visual Basic: now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.