none
Cannot open PR_ATTACH_DATA_OBJ as IStream? RRS feed

  • Question

  • Hi,

    Currently I'm looking for a way to add a file attachment to a message. The file attachment is in stroage structure(Word .doc file)

    1. One way is to use OpenProperty to open the property as

        hr = pAttach->OpenProperty (PR_ATTACH_DATA_OBJ,
                                    &IID_IStorage,
                                    0,
                                    MAPI_MODIFY | MAPI_CREATE,
                                    (LPUNKNOWN *)&p);

    However, the problem is that IStorage does not support to write data into the storage directly, i.e., no Write Method.

    2. Therefore, I am think of opening the property as a IStream interface, as IStream supports to write data into the stream directly:

        hr = pAttach->OpenProperty (PR_ATTACH_DATA_OBJ,
                                    &IID_IStream,
                                    0,
                                    MAPI_MODIFY | MAPI_CREATE,
                                    (LPUNKNOWN *)&p);

    However, I will always get hr = -2147221246, which is not any of the return values listed in https://docs.microsoft.com/en-us/office/client-developer/outlook/mapi/imapiprop-openproperty So I don't know why I cannot open the property as a stream.

    3. I also try to open the property as IID_IStreamDocfile, as below:

        hr = pAttach->OpenProperty (PR_ATTACH_DATA_OBJ,
                                    &IID_IStreamDocfile,
                                    0,
                                    MAPI_MODIFY | MAPI_CREATE,
                                    (LPUNKNOWN *)&p);

    The function will succeed. However, there seems no such a type called LPSTREAMDOCFILE for IID_IStreamDocfile, like LPSTREAM for IID_IStream. Therefore, finally I have to use LPSTREAM for the type of p. Just wonder if it is correctly.

    Also whether I should use IID_IStreamDocFile or IID_IStorage to open the storage file, since both are able to the task? Based on MSDN document at https://docs.microsoft.com/en-us/office/client-developer/outlook/mapi/imapiprop-openproperty, it seems that IStreamDocFile is better, "IStreamDocfile is the best choice for accessing OLE 2.0 attachments because it involves the least amount of overhead."

    Tuesday, January 7, 2020 7:47 AM

All replies

  • If you obtain an IStorage interface pointer for the compound document try using IStorage::CopyTo method to copy to the attachment's IStorage.

    Tuesday, January 7, 2020 10:35 AM
  • It does not matter that a Word document is an OLE storage file, it is still a blob as far as Outlook and MAPI are concerned. Open PR_ATTACH_DATA_BIN as IStream and write the data as if it were any other kind of file.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, January 7, 2020 3:13 PM