I am using late binding to connect to MS Outlook and to open and extract info from outlook emails using the MailItem
object.
I am trying to save attachments to file. This is fairly straightforward in most instances using the Attachment
object and its SaveAsFile
method.
However, it does not work where the Attachment
Type
is olOLE
. I believe this only relates to documents embedded in emails created in RTF format (hopefully few and far between nowadays).
Via the Attachment
object it is possible to access MAPI
properties not exposed by the object model using its PropertyAccessor
.
The relevant MAPI
property for OLE objects is PR_ATTACH_DATA_OBJ
, which can be accessed using the PropertyInspector as in the following example:
Function SaveOLEAttachmentToFile(Attachment:Variant; fn:String): boolean;
var
OPA, PropName : Variant;
begin
Result := false;
OPA := Attachment.PropertyAccessor;
PropName := 'http://schemas.microsoft.com/mapi/proptag/0x3701000D '; //PR_ATTACH_DATA_OBJ
?????? := OPA.GetProperty(PropName);
end;
I am stuck at this point as I can't know work out what Delphi type to save the data to and I am not even sure this is possible having read the MS documentation (Click here). PR_ATTACH_DATA_OBJ
returns a PT_OBJECT
. I am hoping that this object contains the raw data which (if I could work out how to access it in Delphi
) can be simply saved to a file. However, the documentation suggests it may not be that simple and it's possible I may have to work with Extended MAPI
. I have spent a few hours researching the latter with no concrete result other than a headache. I appreciate I could use Redemption, but I don't want to use a third party tool for something which is fairly minor in the round.
If anyone can advise as to a data type to hold the PT_OBJECT
from which it can be simply saved to file that would be my route one.
Failing that, if I need to dig deeper into MAPI
, I would be grateful if anyone could clarify/amplify my research so far. I have the following steps:
- Initialize
MAPI
. - Get an
IMAPIPROP
interface. I think I should be getting the interface from myAttachment
object and the following seems to work (ie compiles and executes without problems):MAPIPROP := IUnknown(Attachment.MAPIObject) as IMAPIPROP
. Failing that, I would have to cast the parentMailItem
toIMAPIPROP
interface and work my way down to the attachment viaGetAttachmentTable
. - Load the attachment data into an
IStream
:if Succeeded(MAPIPROP.OpenProperty(PR_ATTACH_DATA_OBJ, IStream, STGM_READ, 0, IUnknown(SourceStream)) then
- Extract the data from the
IStream
and save to file
I have failed to get as far as point 3 as something would seem to be wrong with my initial casting to IMAPIPROP
albeit it does not cause any violations. I have tried reading a single property from the MailItem
cast to IMAPIPROP
using the following code:
if (Succeeded(HrGetOneProp(MAPIPROP, PR_SUBJECT, Prop))) then
And I get an access violation. Likewise if I cast the Attachment
object and query an attachment property I also get a violation. I don't think the problem lies with the call to HrGetOneProp
, I think it has to be the casting to IMAPIPROP.
Any pointers re the above would be greatly appreciated.