Reading File Attachments

Once the code has a reference to a Message object, it’s easy to iterate through the object’s Attachments collection to read its attachments. The Attachment object’s properties of interest are Name, Position, Type, and Source. These properties are similar in use to the like-named parameters of the Attachments collection’s Add method, discussed earlier in this chapter.

Attachments with their Type property equal to CdoFileData are file attachments. The Name property of file attachments indicates the display name that should be shown when rendering a representation of the attachment in the client UI. It is also a suggested filename to be used if the file is to be saved to disk (discussed shortly). The Position property is an index into the text of the message. It is the character position at which the attachment representation should be rendered. The Source property is the full path and filename of the file on the sender’s system, not useful to the receiver.

When a message with file attachments arrives, the attachments are stored in the recipient’s message store. To copy a file attachment to the recipient’s local file system, the client software must call the Attachment object’s WriteToFile method. The syntax of the WriteToFile method is:

            CdoAttachment.WriteToFile(FileName)

The FileName parameter is a Variant/String that specifies the full path and filename of the file to be created. If the file already exists, it is overwritten without warning.

Example 6-5 ...

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.