Question

I have a program that reads and send emails using Outlook automation, from Visual FoxPro.

I have functions to retrieve and save attachments, but I would like to read the contents of an attachment to a string without saving the file to disk.

In the example below, I instantiate Outlook, pick up a namespace and a reference to the current user's inbox. I then pick up a reference to an arbitrary message (number 32 in the inbox), and a reference to the message's attachment.

oOutlook = createobject('Outlook.Application')
oNamespace = oOutlook.GetNamespace('MAPI')
oInbox = oNamespace.GetDefaultFolder(olFolderInbox)
oMessage = Inbox.Items[32]
oAttachment = oMessage.Attachments[1]

At this point I would call:

oAttachment.SaveAsFile('C:\Example\File1.xml')

to save the file to disk.

What I want to do is to read the contents of the file to a string, without saving it to disk.

In this particular instance, the file is XML.

Was it helpful?

Solution 2

Unfortunately there is no way to store an Outlook attachment to memory without using third party libraries, if it is greater than 8KB in size.

In Outlook 2007, you can call Attachment.PropertyAccessor.GetProperty to read the PR_ATTACH_DATA_BIN property to memory if the attachment is less than 8KB.

As mentioned, Redemption is the best bet if you were to use a third-party library.

OTHER TIPS

If you were using C++ or MAPI, you could use Extended MAPI to open the PR_ATTACH_DATA_BIN property as IStream.

In VFP you can try Redemption (I am its author) - both Safe*Item and RDO family of objects expose the AsArray property (as well as AsText and AsStream).

In Outlook 2007 and above you can also use Attachment.PropertyAccessor.GetProperty to read PR_ATTACH_DATA_BIN property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x37010102")

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top