Monday, June 02, 2008

Embedded images (aka Attachment.Type = olOLE) in Outlook messages and one way to get at them...

Jelle Druyts - Extracting OLE embedded images from emails in Outlook

"While it seemed a simple requirement, saving all attachments from emails in Outlook to disk proved to be challenging - to say the least. Using the Outlook Object Model, it's quite easy to enumerate all emails in a folder, look at their attachments and call the SaveAsFile method on them. However, for OLE-type attachments (typically images), this throws a COMException saying: "Outlook cannot do this action on this type of attachment". While looking for alternatives or workarounds, I found nothing but confirmation that this is indeed not an easy task - even from Dmitry Streblechenko, Outlook MVP and creator of the excellent and very affordable Outlook Redemption library: "If you mean embedded graphics objects in the RTF messages, there is not much you can do [...] You can look at the storage itself to figure that out, but I've never tried that".

Ultimately, after lots of trial and error, I did manage to find a fairly easy way to save these OLE embedded images by (mis)using the clipboard. Basically, I retrieve the attachment’s IStorage OLE interface (available through Redemption) and call OleLoad on it to have OLE load the contents and retrieve an IDataObject. The magic trick is to place that IDataObject on the clipboard and retrieve the actual image from the clipboard (so that the clipboard itself handles the nasty OLE details).

..."

Getting at olOLE attachments in Outlook can be a major pain. Jelle's method, while may seem "hacky", gets the job done and should be pretty reliable ("just barely good enough to get the job done" as I call it, is still getting the job done...).

On a related note, if you're doing Outlook development, you need to take a long hard look at Outlook Redemption. I've been using it for years and it makes the impossible by mortals, possible. It's also how some third party utilities do some of their magic (for instance Xobni uses it). And best of all, Dmitry provides outstanding and invaluable support...

1 comment:

Unknown said...

Thanks for posting this information. I looked around for a long time and all posts of code stated to simply use SaveAsFile on the Attachment collection, but this was obviously not available for exactly the reason you stated. Thanks for confirming the difficulty with this and posting a workaround.