Search code examples
xmlautomationoutlookvisual-foxpro

VFP: How can I access the contents of an Outlook attachment


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.


Solution

  • 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.