Put Data to Clipboard to Paste it anywhere

By | Last Updated on May 5, 2013 | 11,559

Use method CLIPBOARD_EXPORT of class CL_GUI_FRONTEND_SERVICES to put data into your clipboard which can be copied anywhere like Notepad, Excel, etc.

You can put the data into clipboard using the method CLIPBOARD_EXPORT. Once you have data in clipboard, you can use Ctrl+V or Paste to paste data from clipboard to notepad, wordpad, excel etc.

Put data into Clipboard

 
*
TYPES:
  BEGIN OF ty_data,
    fld1 TYPE char20,
    fld2 TYPE char20,
    fld3 TYPE char20,
  END   OF ty_data.
DATA: lt_data TYPE STANDARD TABLE OF ty_data.
FIELD-SYMBOLS: <lfs_data>  LIKE LINE OF lt_data.
 
START-OF-SELECTION.
 
  DO 10 TIMES.
    APPEND INITIAL LINE TO lt_data ASSIGNING <lfs_data>.
    <lfs_data>-fld1 = sy-abcde+0(10).
    <lfs_data>-fld2 = sy-index.
    CONDENSE <lfs_data>-fld2.
    CONCATENATE '_123_' <lfs_data>-fld2 INTO <lfs_data>-fld3.
  ENDDO.
*
* TO hold buffer to put in clipboard
  TYPES:
    BEGIN OF ty_clipdata,
      data TYPE c LENGTH 500,
    END   OF ty_clipdata.
  DATA: lt_clipdata TYPE STANDARD TABLE OF ty_clipdata.
  DATA: ls_clipdata LIKE LINE OF lt_clipdata.
  CONSTANTS: c_tab  TYPE c VALUE cl_bcs_convert=>gc_tab.
  DATA: lv_rc TYPE i.
*
* Add horizontal tab to add paste data in Excel
  LOOP AT lt_data ASSIGNING <lfs_data>.
    CONCATENATE <lfs_data>-fld1
                <lfs_data>-fld2
                <lfs_data>-fld3
      INTO ls_clipdata-data
      SEPARATED BY c_tab.
    APPEND ls_clipdata TO lt_clipdata.
  ENDLOOP.
*
* Put into clipboard
  cl_gui_frontend_services=>clipboard_export(
    IMPORTING
      data                 = lt_clipdata
    CHANGING
      rc                   = lv_rc
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      no_authority         = 4
      OTHERS               = 5 )
          .
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
 

Use Paste or Ctrl+V on excel or notepad, to paste the content. Should be similar to:

 
ABCDEFGHIJ	1	_123_1
ABCDEFGHIJ	2	_123_2
ABCDEFGHIJ	3	_123_3
ABCDEFGHIJ	4	_123_4
ABCDEFGHIJ	5	_123_5
ABCDEFGHIJ	6	_123_6
ABCDEFGHIJ	7	_123_7
ABCDEFGHIJ	8	_123_8
ABCDEFGHIJ	9	_123_9
ABCDEFGHIJ	10	_123_10
 

Related Code Snippet – Get Data from Clipboard

Do you have a Code Snippet which you want to share, Submit Code Snippet here

Tags

Share It!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

3 Comments

  • steve oldner

    I have used this a few years before. Very good reminder.

    Thanks!

  • Jurgen

    I’ve actually encapsulated this in a class called clipboard.

    It has a simple constructor that pulls the data in and splits it in up to 10 columns. You can tell it if the data has a header or not.

    I can then do things like:

    if clipboard->is_empty( ) eq abap_true …
    or
    loop at clipboard->data…

    I hope this is interesting to someone.
    Jurgen

  • Hello Jurgen,

    Nice Trick. Thanks for sharing.

    Thanks,
    Naimesh Patel

Comments on this Post are now closed. If you have something important to share, you can always contact me.