'How to access EmbeddedItem attachment in Outlook VBA
I am using Outlook 2010 and the QuickSteps only allow to create a Task from an email with the email body copied to the task body or with the email as attachment.
I already have a small script which is called when a new task object is added to the task directory. If I have a an email attachment to the task I would like to copy its body to the task body.
Public WithEvents OlItems As Outlook.Items
Sub Application_Startup()
Set OlItems = Session.GetDefaultFolder(olFolderTasks).Items
End Sub
Sub OlItems_ItemAdd(ByVal Item As Object)
Dim obApp As Application
If Item.Class = olTask Then
Item.Status = olTaskDeferred
If Item.Attachments.Item(1).Type = olEmbeddeditem Then
Dim attachment As attachment
Set attachment = Item.Attachments.Item(1)
Debug.Print (attachment.FileName)
'???: Item.body = attachment.body
End If
Item.Save
End If
Set obApp = Nothing
End Sub
The attachment is of type olEmbeddedItem. I can't figure out how to open/read from it?
Is this possible? My goal is to have both body and email attachment in the task item.
Solution 1:[1]
Outlook Object Model will not let you directly access embedded message attachments - if you were using C++ or Delphi, you could use Extended MAPI to open the embedded message attachment using IAttach::OpenProperty(PR_ATTACH_DATA_OBJ, IID_IMessage)
.
The only workaround is to save the attachment as an MSG file (Attachment.SaveAsFile
) and open it using Application.Session.OpenSharedItem
Or you can use Redemption (I am its author - it wraps Extended MAPI and can be used from any language) and do something like the following:
RDOSession session = new RDOSession();
RDOMail msg = (RDOMail)session.GetRDOObjectFromOutlookObject(YourOutlookMailItem);
foreach (RDOAttachment att in msg.Attachments)
{
if (att.Type == rdoAttachmentType.olEmbeddedItem)
{
RDOMail embeddedMsg = att.EmbeddedMsg;
ProcessEmbeddedMessage(embeddedMsg);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |