Outlook messages from SAP system

This is a sample code that can be used to create draft messages in your Outlook through SAP. Of course it is important to understand what it does so I tried to comment as much I can.

The VB code that can be used to create draft message is given here:

Sub create_draftmsg()
  Dim oApp As Outlook.Application
    Dim oEmail As Outlook.MailItem
    Set oApp = New Outlook.Application
    Set oEmail = oApp.CreateItem(olMailItem)
With oEmail
        .To = "to@mail.com"
        .CC = ""
        .Subject = "SUBJECT"
        .Body = "BODY TEXT"
        .Attachments.Add "C:\Cat.bmp", olByValue
        .Save
    End With
    Set oEmail = Nothing
End Sub

The ABAP code is given below.

INCLUDE OLE2INCL.
* they are all OLE2_OBJECTS so it is important to know the real classes
DATA: OUTLOOK TYPE OLE2_OBJECT,
MAILITEM TYPE OLE2_OBJECT,
ATTACHMENTS TYPE OLE2_OBJECT,
APPT TYPE OLE2_OBJECT,
DESTI TYPE OLE2_OBJECT,
BODYMAIL(600) TYPE C.
CREATE OBJECT OUTLOOK 'OUTLOOK.APPLICATION'.
* Get the outlook namespace
CALL METHOD OF OUTLOOK 'GetNamespace' = NAMESPACE
EXPORTING
#1 = 'MAPI'.
* Get outlook folders and create a reference to sent mail folder
* (myfolder)
* If this is not used than a draft message is not created when Outlook is not running
CALL METHOD OF NAMESPACE 'GetDefaultFolder' = MYFOLDER  EXPORTING     #1 = 16.    "olFolderDraftMail (value from a short debugging in VB)
CALL METHOD OF OUTLOOK 'CreateItem' = OUTMAIL EXPORTING #1 = 0. " 0 - MailItem
SET PROPERTY OF OUTMAIL 'SUBJECT' = 'SUBJECT'.
SET PROPERTY OF OUTMAIL 'BODY' = BODYMAIL.
CALL METHOD OF OUTMAIL 'RECIPIENTS' = DESTI.
CALL METHOD OF DESTI 'ADD' EXPORTING #1 = 'recipient@mail.com'.
CALL METHOD OF OUTMAIL 'ATTACHMENTS' = ATTS.
CALL METHOD OF ATTS 'ADD' EXPORTING #1 = 'filepath:\file.type'.
CALL METHOD OF OUTMAIL 'SAVE'.
FREE OBJECT DESTI.
FREE OBJECT ATTS.
FREE OBJECT OUTMAIL.
FREE OBJECT OUTLOOK.

Today’s wisdom: Use Macro recorder to discover the classes needed in order to use Microsoft goodies in ABAP! 🙂