[ABAP] Spoolauftrag erzeugen und Text in den SAP-Spool ausgeben

DATA: lv_spoolid TYPE tsp01-rqident.
DATA: lv_handle TYPE sy-tabix.

* Spool: Öffnen eines Spoolauftrags zum Schreiben
CALL FUNCTION 'RSPO_SR_OPEN'
  EXPORTING
    dest             = 'LOCL'        " Ausgabeziel ist lokaler Drucker (Windows)
    layout           = 'X_65_80'
    titleline        = 'Spoolausgabe'
    doctype          = 'LIST'
  IMPORTING
    handle           = lv_handle
    spoolid          = lv_spoolid
  EXCEPTIONS
    device_missing   = 1
    name_twice       = 2
    no_such_device   = 3
    operation_failed = 4
    OTHERS           = 5.

IF sy-subrc = 0.

  WRITE: 'Open spool:', lv_spoolid.

  DATA: lv_text TYPE char255.
  lv_text = 'Testtext'.

* Spool: Schreiben einer Zeile in einen offenen Spoolauftrag
  CALL FUNCTION 'RSPO_SR_WRITE'
    EXPORTING
      handle           = lv_handle
      text             = lv_text
    EXCEPTIONS
      handle_not_valid = 1
      operation_failed = 2
      OTHERS           = 3.

  IF sy-subrc = 0.
    WRITE: / 'Text written.'.
  ELSE.
    WRITE: / 'Writing failed.'.
  ENDIF.

  IF sy-subrc = 0.
* Spool: Schliessen eines Spoolrequests
    CALL FUNCTION 'RSPO_SR_CLOSE'
      EXPORTING
        handle           = lv_handle
      EXCEPTIONS
        handle_not_valid = 1
        operation_failed = 2
        OTHERS           = 3.

    IF sy-subrc = 0.
      WRITE: / 'Close.'.
    ELSE.
      WRITE: / 'Close failed.'.
    ENDIF.
  ENDIF.
ELSE.
  WRITE: / 'Open failed.'.
ENDIF.