cancel
Showing results for 
Search instead for 
Did you mean: 

Open Outlook email editor from SAP

0 Kudos

Hi,

I need to open the outlook email editor from ABAP program so that the user can manually input the To, CC, subject, body and attachment.

Can anybody help ?

Thanks and regards

Arijit Ghose

Accepted Solutions (1)

Accepted Solutions (1)

former_member182670
Contributor
0 Kudos

REPORT  ZTESTTTTT                               .

INCLUDE ole2incl .
DATA: lr_outlook TYPE OLE2_OBJECT,
      lr_mi TYPE OLE2_OBJECT.

CREATE OBJECT lr_outlook 'Outlook.Application'.

CALL METHOD OF lr_outlook 'CreateItem' = lr_mi
  EXPORTING #1 = 0.

CALL METHOD OF lr_mi 'Display'.

FREE OBJECT lr_outlook.
0 Kudos

Hi,

Thanks a lot. But, how do I enter values in To CC, Subjectand Body ?

Arijit

former_member182670
Contributor
0 Kudos

REPORT ztesttttt .

INCLUDE ole2incl .

DATA: lr_outlook TYPE ole2_object,

lr_mi TYPE ole2_object.

CREATE OBJECT lr_outlook 'Outlook.Application'.

CALL METHOD OF lr_outlook 'CreateItem' = lr_mi

EXPORTING #1 = 0.

SET PROPERTY OF lr_mi 'To' = 'your@to.address'.

SET PROPERTY OF lr_mi 'CC' = 'your@cc.address'.

SET PROPERTY OF lr_mi 'Subject' = 'Your subject'.

SET PROPERTY OF lr_mi 'Body' = 'Some text'.

CALL METHOD OF lr_mi 'Display'.

FREE OBJECT lr_outlook.

0 Kudos

Hi Tomasz,

It is super !! I need one more help.

Can I make the body multiline ?

Arijit

0 Kudos

Hi Tomasz,

Its great. How can I add a more than one line to body ?

also, the default signature set in outlook is not coming in this case. Please help. I have sent another email earlier.

Arijit

former_member182670
Contributor
0 Kudos

Here is an example with multiline body and default signature.


DATA: lr_outlook TYPE ole2_object,
      lr_mi TYPE ole2_object,
      lv_body TYPE string,
      lv_sig type string.

CREATE OBJECT lr_outlook 'Outlook.Application'.

CALL METHOD OF lr_outlook 'CreateItem' = lr_mi
  EXPORTING #1 = 0.

SET PROPERTY OF lr_mi 'To' = 'your@to.address'.
SET PROPERTY OF lr_mi 'CC' = 'your@cc.address'.
SET PROPERTY OF lr_mi 'Subject' = 'Your subject'.

CALL METHOD OF lr_mi 'Display'.


GET PROPERTY OF lr_mi 'Body' = lv_sig.



CONCATENATE 'First line' cl_abap_char_utilities=>newline
  INTO lv_body.
CONCATENATE lv_body 'Second line' cl_abap_char_utilities=>newline
  INTO lv_body.
CONCATENATE lv_body 'Third line' cl_abap_char_utilities=>newline
  INTO lv_body.

CONCATENATE lv_body lv_sig  INTO lv_body.

SET PROPERTY OF lr_mi 'Body' = lv_body.

FREE OBJECT lr_outlook.

Note that Outlook shows confirmation dialog box when a solution tries to programmatically access the following properties of the MaiItem object:

SentOnBehalfOfName

SenderName

ReceivedByName

ReceivedOnBehalfOfName

ReplyRecipientNames

To

CC

BCC

Body

HTMLBody

Recipients

SenderEmailAddress

So when you read the Body property to get default signature Outlook will ask for confirmation.

Please reward the posts that are helpful.

0 Kudos

Hi Tomasz,

Thanks a million !!

MY email id is arijit.ghose@cognizant.com

What is yours ?

Thanks and regards

Arijit Ghose

athavanraja
Active Contributor
0 Kudos

Hi Arijit,

Sorry for the delay. here is the topinclude code for the function i have mentioned. But Tomasz Mackowski has shown a better method and i guess its better to use that rather than this vb script method.

data: begin of int_receivers occurs 5.         " Receipients
               include structure soos1.
data: end   of int_receivers.

tables: sood,           " SAPoffice: Object definition
        soid,
        soud,
        tsp01.
data: begin of object_id.                " Identifies the object type
               include structure soodk.
data: end   of object_id.
data: begin of object_hd_change.
               include structure sood1.
data: end   of object_hd_change.

data: begin of folder_id.
               include structure soodk.
data: end   of folder_id.

data: begin of name_out.                   " SAP usernames & mail users
               include structure soud3.
data: end   of name_out.

data: rcode     like sy-subrc.

data: begin of objhead occurs 0.
               include structure soli.
data: end   of objhead.

data: begin of objpara occurs 0.
               include structure selc.
data: end   of objpara.

data: begin of objparb occurs 0.
               include structure soop1.
data: end   of objparb.

data: begin of receivers occurs 5.         " Receipients
               include structure soos1.
data: end   of receivers.
data: r_lines type p.               " Number of receipients

*data for Z_V_ARABIC_TRANS
data: wf_cntr1 like sy-tabix value 0,
      wf_cntr2 like sy-tabix value 0,
      wf_fld1(128),
      wf_fld2(128),
      wf_flen like sy-tabix,
      wf_index like sy-tabix value 0,
      wf_onechar(1),
      wf_onehex type x,
      wg_arabic(1),
      wf_offset like sy-tabix,   " Benin pointer
      wf_length like sy-tabix,   " End pointer
      wf_hex_inchar(2),
      wg_subrc like sy-subrc.

data: begin of int_word occurs 0,
        word(128),
      end of int_word.
data: begin of int_field occurs 0,
        offset like sy-tabix,
        length like sy-tabix,
      end of int_field.

Regards

Raja

Former Member
0 Kudos

hi athavan. can i have ur email id please?

tks.

samant

athavanraja
Active Contributor
former_member182670
Contributor
0 Kudos

You can find my email in <a href="https://www.sdn.sap.com:443http://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.sdn.businesscard.sdnbusinesscard?u=ggzzeye%2by6dpfoejrjqnvg%3d%3d">bc</a>

0 Kudos

Dear Tomasz,

Thanks for your code. It is working--can you please also provide code for adding attachments from the local system? Like we would like to attach a file from c:\temp directory?

Thank you,

Vijay

former_member198834
Participant
0 Kudos

This message was moderated.

ŁukaszPęgiel
Contributor

Answers (2)

Answers (2)

0 Kudos

Hi,

Unable to open the Outlook from NWBC using similar code.

Any help would be appreciated.

Thanks

Saifuddin Shaik

athavanraja
Active Contributor
0 Kudos

check out this sample code (using VB script) , you can do similar stuff using activex control as well.

FUNCTION Z_OUTLOOK_MAIL_CREATE.
*"----------------------------------------------------------------------
*"*"Local interface:
*"       IMPORTING
*"             VALUE(I_SUBJECT) TYPE  C OPTIONAL
*"       TABLES
*"              EMAIL_BODY STRUCTURE  SOLI OPTIONAL
*"              ADDRESS_LIST STRUCTURE  SOLI OPTIONAL
*"              ATTACHMENTS STRUCTURE  SOLI OPTIONAL
*"       EXCEPTIONS
*"              INVALID_PATH
*"              DOWNLOAD_FAILED
*"              EXECUTION_FAILED
*"----------------------------------------------------------------------
  DATA:
    V_TRANSLATE(2),
    IT_VBS          LIKE SOLI
       OCCURS 50 WITH HEADER LINE,
    V_LAST,
    V_VBS_FILENAME  LIKE RLGRAP-FILENAME,
    COMMANDLINE(1000).
*- Prepare a code to translate a quote into a hex-tab
*- so it can then be translated back to 2 double quotes.
  CONCATENATE '"' CON_HEX-TAB INTO V_TRANSLATE.

  APPEND:
    'Dim myolapp                                        ' TO IT_VBS,
    'Dim olNamespace                                    ' TO IT_VBS,
    'Dim myItem                                         ' TO IT_VBS,
    'Dim myRecipient                                    ' TO IT_VBS,
    'Dim myAttachments                                  ' TO IT_VBS,
    '                                                   ' TO IT_VBS,
    'Set myolapp = CreateObject("Outlook.Application")  ' TO IT_VBS,
    'Set olNamespace = myolapp.GetNamespace("MAPI")     ' TO IT_VBS,
    'Set myItem = myolapp.CreateItem(olMailItem)        ' TO IT_VBS,
    '                                                   ' TO IT_VBS.

*- Translate the body into a single line.
  LOOP AT ADDRESS_LIST.
    CONCATENATE
      'Set myRecipient = myItem.Recipients.Add("'
      ADDRESS_LIST
      '")'
      INTO IT_VBS.
    APPEND IT_VBS.
  ENDLOOP.

  APPEND:
    'myItem.VotingOptions = "Approve;Reject"' TO IT_VBS.
*- Build the subject line.
  CONCATENATE
    'myItem.Subject = "'
    I_SUBJECT
    '"'
    INTO IT_VBS.
  APPEND IT_VBS.
*- Prepare attachments
  APPEND:
    'Set myAttachments = myItem.Attachments' TO IT_VBS.
*- Check if the attachment exists
  LOOP AT ATTACHMENTS.
    CALL FUNCTION 'WS_QUERY'
         EXPORTING
              FILENAME       = ATTACHMENTS
              QUERY          = 'FE'
         EXCEPTIONS
              INV_QUERY      = 1
              NO_BATCH       = 2
              FRONTEND_ERROR = 3
              OTHERS         = 4.
    IF SY-SUBRC EQ 0.
      CONCATENATE 'myAttachments.Add("'
                  ATTACHMENTS
                  '")'
        INTO IT_VBS.
      APPEND IT_VBS.
* '    olByValue, 1, "4th Quarter 1996 Results Chart"'
*      append '    olByReference, 1' to it_vbs.
    ELSE.
      MESSAGE I017(ZZ) WITH
         'Could not attach' ATTACHMENTS.
    ENDIF.
  ENDLOOP.

*- Prepare the email body.
  CLEAR: V_LAST, IT_VBS.
  APPEND IT_VBS.
  LOOP AT EMAIL_BODY.
    AT FIRST.
      APPEND 'myitem.body = _' TO IT_VBS.
    ENDAT.
    AT LAST.
      V_LAST = 'X'.
    ENDAT.

    TRANSLATE EMAIL_BODY USING V_TRANSLATE.
    WHILE SY-SUBRC EQ 0.
      REPLACE CON_HEX-TAB WITH '""' INTO EMAIL_BODY.
    ENDWHILE.

    IF V_LAST = 'X'.
      CONCATENATE '"' EMAIL_BODY '" &vbCrLf '
        INTO IT_VBS.
    ELSE.
      CONCATENATE '"' EMAIL_BODY '" &vbCrLf  &_'
        INTO IT_VBS.
    ENDIF.
    APPEND IT_VBS.
  ENDLOOP.

  APPEND 'myItem.Display' TO IT_VBS.

*- Prepare the vbscript filename for download and execution
  CLEAR V_VBS_FILENAME.
  CALL FUNCTION 'WS_QUERY'
       EXPORTING
            ENVIRONMENT    = 'TEMP'
            QUERY          = 'EN'
       IMPORTING
            RETURN         = V_VBS_FILENAME
       EXCEPTIONS
            INV_QUERY      = 1
            NO_BATCH       = 2
            FRONTEND_ERROR = 3
            OTHERS         = 4.
  IF SY-SUBRC <> 0.
    RAISE INVALID_PATHNAME.
  ENDIF.
  CONCATENATE V_VBS_FILENAME 'mail.vbs'
    INTO V_VBS_FILENAME.
  COMMANDLINE = V_VBS_FILENAME.
*- Download the file
  CALL FUNCTION 'WS_DOWNLOAD'
       EXPORTING
            FILENAME            = V_VBS_FILENAME
            FILETYPE            = 'DAT'
            MODE                = 'S'
       TABLES
            DATA_TAB            = IT_VBS
       EXCEPTIONS
            FILE_OPEN_ERROR     = 1
            FILE_WRITE_ERROR    = 2
            INVALID_FILESIZE    = 3
            INVALID_TABLE_WIDTH = 4
            INVALID_TYPE        = 5
            NO_BATCH            = 6
            UNKNOWN_ERROR       = 7
            OTHERS              = 8.
  IF SY-SUBRC <> 0.
    RAISE DOWNLOAD_FAILED.
  ENDIF.

  CALL FUNCTION 'WS_EXECUTE'
       EXPORTING
            COMMANDLINE    = COMMANDLINE
            PROGRAM        = 'WSCRIPT.EXE'
       EXCEPTIONS
            FRONTEND_ERROR = 1
            NO_BATCH       = 2
            PROG_NOT_FOUND = 3
            ILLEGAL_OPTION = 4
            OTHERS         = 5.
  IF SY-SUBRC <> 0.
    RAISE EXECUTION_FAILED.
  ENDIF.

ENDFUNCTION.

Regards

Raja

0 Kudos

Hi Raja,

Thanks a lot. But, did you use some structures, variables, etc declared in some other includes ? I am

getting syntax errors.

Arijit