Freigeben über


Unable to open Outlook item attached using Attachments.Add and using the olByReference flag on the Outlook 2007

One of our customer is sending Outlook item attached using Attachments.Add with olByReference Flag on Outlook 2007. If the recipient of the email tries to open the attached item

will get “Microsoft Office Oulook could not complete the operation. One or more parameters values are not valid” exception.

OlAttachErr

To repro the issue we can use following sample VBA code snippet:

NOTE: Following programming examples is for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This sample code assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. This sample code is provided for the purpose of illustration only and is not intended to be used in a production environment.

 Sub SendCurrentItemAsAttach()
     Set currItem = Application.ActiveInspector.CurrentItem
     currItem.Save
     Set RequestItem = Application.CreateItem(0)
     RequestItem.Subject = "Please check attached mail"
     RequestItem.HTMLBody = "Please check attached mail" & Chr(13) 
     RequestItem.To = "test@domain.com" 
     Set myAttachments = RequestItem.Attachments
     myAttachments.Add currItem, 4 'olByReference(4)
     RequestItem.Send
     Set RequestItem = Nothing
 End Sub

The above issue is happening because of the design changes in the Outlook 2007.

One workaround to address the above issue is to include a hyperlink in the email using the Outlook:\\<ENTRYID> protocol to link to the existing item rather than attaching the item ByRef. So to avoid above issue we can use following VBA sample code snippet:

 Sub SendCurrentItemAsAttach()
     Set currItem = Application.ActiveInspector.CurrentItem
     currItem.Save
     Set RequestItem = Application.CreateItem(0)
     RequestItem.Subject = "Please check attached mail"
     RequestItem.HTMLBody = "Please click to open the requestl" & Chr(13) & "<a href=Outlook:\\" & currItem.EntryID & ">Click Mail</a>"
     RequestItem.To = "test@domain.com"
     RequestItem.Send
     Set RequestItem = Nothing
 End Sub

IMP: using Outlook:EntryID approach but it would works ONLY in case of HTML/RTF mail.

References: