In previous post I've shown how to zip file directly on PC without using CL_ABAP_ZIP, this time I will show you how easy is to use OLE2_OBJECT to create an MS Outlook message with attachment and save it in Outlook's Drafts folder. OLE2_OBJECT in fact can be used with any application that allows it, but today I will only show a hint how to work with MS Outlook.

I know that most of us use CL_BCS to create and send the message directly from SAP, but in a situation when you're working on local files then It may be better to do it with Outlook. This is of course just a short sample of the possibilities, all functions for OLE & MS you can find in MSDN documents.

Here is the code with f_path parameter which is used to pass path of local file to attach to message:

form outlook using f_path type csequence.

  datafo_appoutlook type ole2_object.
  datafo_appitem    type ole2_object.
  datafo_namespace  type ole2_object.
  datafo_attachments type ole2_object.
  datafo_inspector type ole2_object.

*  Create  outlook.application
  create object fo_appoutlook 'outlook.application' .

  call method of fo_appoutlook 'GetNameSpace' fo_namespace
      #1 'MAPI'.

*  create item
  call method of fo_appoutlook 'CreateItem' fo_appitem
      #1 '0'.

  call method of fo_appitem 'GetInspector' fo_inspector.
*  exporting
*    #1 = '0'.
*  set body of an email
set property of fo_appitem 'Htmlbody' 'This is body<br>Use HTML'.

*  set recipients, can be separated with ; if more than one needed
   set property of fo_appitem 'To'      'This email address is being protected from spambots. You need JavaScript enabled to view it.'.
*  set subject
  set property of fo_appitem 'Subject' =  'New Mail'.
*  add attachments
  if f_path is not initial.
    call method of fo_appitem 'ATTACHMENTS' fo_attachments.
    call method of fo_attachments 'ADD'
        #1 f_path.

*  you can diplay window or Save it in Drafts
*  call method of fo_appitem 'Display'.
  call method of fo_appitem 'Save'.

  free object fo_appitem.
  free object fo_namespace.
  free object fo_appoutlook.

endform.                    "outlook