Sending Embedded OLE Objects

It’s technically possible, but difficult, to send an embedded OLE attachment using the MAPI controls. (OLE is a mechanism that allows users to create and edit documents containing items or “objects” created by multiple applications. In the context of sending email, an embedded OLE attachment is an OLE object—for example, a range of cells from a spreadsheet—that is transmitted along with the message. A general discussion of OLE is not within the scope of this book; consult Microsoft documentation for further information about this technology.)

If you need to send embedded OLE attachments, you’re better off using Collaboration Data Objects (CDO). However, I’ll explain briefly how it could be done with the MAPI controls.

As explained in the previous section, sending a file attachment requires setting the AttachmentType property of the MAPIMessages control to mapData. There are two other legal values for this property: mapEOLE and mapSOLE. Either value can be used to send an OLE object. The difference is that mapEOLE is for an “editable” OLE object, and mapSOLE is for a “static” (i.e., non-editable) OLE object.

On the receiving end, the mail client displays the OLE object within the body of the message. If the AttachmentType property was set to mapEOLE, the user can double-click the object to begin editing it. For this to work, the user must have software on his or her system that knows how to edit the kind of object that was sent. For example, if you send an OLE object that contains a range of cells from a Microsoft Excel spreadsheet, the receiving user must have Excel in order to edit the object. However, it is not necessary to have Excel simply to view the object. (OLE objects carry inside them a static visual representation of their data.)

To attach the OLE object to an outgoing message, you must serialize the object into a file, and set the AttachmentPathName property of the MAPIMessages control to the fully qualified path of the file. Serialization is the process of copying an object’s state to some kind of sequential storage device, such as a file. Copying to a non-volatile storage device, such as a file, is also known as persisting the object. The final step is to set the AttachmentName property of the MAPIMessages control equal to the name of the object’s class—for example, "Excel.Sheet“.

The hard part of all this, of course, is serializing the OLE object. The MAPI controls provide no facility for doing this, so you’re forced to call into the Windows API. While I won’t go into details, the basic idea is to open the file as an OLE stream and pass the IStream interface pointer to the OleSaveAsStream API. As I mentioned before, if you’re going to these lengths, you’re better off making the jump to CDO.

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.