Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
ipravir
Active Contributor

Hello,

Here is a sample program to open an Excel or Word Document in SAP Screen.

In the program two functionality has been defined,

1. Open a new MS Excel or Word Document File in SAP Screen or

2. Open any existing MS Excel or Word Document

Program: -

*&---------------------------------------------------------------------*

*& Report  ZPRV_OPEN_EXL_OR_DOC

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

REPORT  ZPRV_DISPLAY_EXCEL.

TYPE-POOLS: SOI.

DATA: CL_CON    TYPE REF TO I_OI_CONTAINER_CONTROL,

      CL_DOC    TYPE REF TO I_OI_DOCUMENT_PROXY,

      CL_WRD    TYPE REF TO I_OI_WORD_PROCESSOR_DOCUMENT,

      CL_SHT    TYPE REF TO I_OI_SPREADSHEET,

      CL_ERR    TYPE REF TO I_OI_ERROR.

DATA: FILE      TYPE C LENGTH 256,

      RETCODE   TYPE SOI_RET_STRING,

      G_UCOMM   TYPE SY-UCOMM.

SELECTION-SCREEN:   BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.

SELECTION-SCREEN: BEGIN OF LINE.

PARAMETERS:   RB_DOC  RADIOBUTTON GROUP R1.

SELECTION-SCREEN: COMMENT 10(40) TXT_DOC FOR FIELD RB_DOC.

SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: BEGIN OF LINE.

PARAMETERS:   RB_XLS  RADIOBUTTON GROUP R1.

SELECTION-SCREEN: COMMENT 10(40) TXT_XLS FOR FIELD RB_XLS.

SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: BEGIN OF LINE.

SELECTION-SCREEN: COMMENT 3(40) TXT_FIL FOR FIELD P_FILE.

PARAMETERS:   P_FILE  TYPE FILE.

SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN:   END OF BLOCK B1.

INITIALIZATION.

  TXT_DOC = 'MS Document File'.

  TXT_XLS = 'MS Excel File'.

  TXT_FIL = 'File Name'.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.

  PERFORM SUB_GET_FILE.

START-OF-SELECTION.

  CALL SCREEN 9000.

*&---------------------------------------------------------------------*

*&      Module  STATUS_9000  OUTPUT

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

MODULE STATUS_9000 OUTPUT.

  IF CL_CON IS NOT INITIAL.

    CALL METHOD CL_CON->DESTROY_CONTROL

*  EXPORTING

*    NO_FLUSH = ' '

      IMPORTING

        ERROR    = CL_ERR

        RETCODE  = RETCODE

        .

  ENDIF.

  CALL METHOD C_OI_CONTAINER_CONTROL_CREATOR=>GET_CONTAINER_CONTROL

    IMPORTING

      CONTROL = CL_CON.

  CALL METHOD CL_CON->INIT_CONTROL

    EXPORTING

      R3_APPLICATION_NAME      = 'R/3 Basis'

      INPLACE_ENABLED          = 'X'

      INPLACE_SCROLL_DOCUMENTS = 'X'

      PARENT                   = CL_GUI_CUSTOM_CONTAINER=>DEFAULT_SCREEN

      REGISTER_ON_CLOSE_EVENT  = 'X'

      REGISTER_ON_CUSTOM_EVENT = 'X'

      NO_FLUSH                 = 'X'.

  IF RB_XLS EQ 'X'.

    CALL METHOD CL_CON->GET_DOCUMENT_PROXY

      EXPORTING

        DOCUMENT_TYPE  = 'Excel.sheet'

        NO_FLUSH       = 'X'

      IMPORTING

        DOCUMENT_PROXY = CL_DOC.

  ELSEIF RB_DOC EQ 'X'.

    CALL METHOD CL_CON->GET_DOCUMENT_PROXY

      EXPORTING

        DOCUMENT_TYPE  = 'Word.Document'

        NO_FLUSH       = 'X'

      IMPORTING

        DOCUMENT_PROXY = CL_DOC.

  ENDIF.

  IF P_FILE EQ SPACE.

    CALL METHOD CL_DOC->CREATE_DOCUMENT

      EXPORTING

        OPEN_INPLACE   = 'X'

        DOCUMENT_TITLE = 'PRV'

        NO_FLUSH       = 'X'.

  ELSE.

    CONCATENATE 'FILE://' P_FILE INTO FILE.

    CALL METHOD CL_DOC->OPEN_DOCUMENT

      EXPORTING

        DOCUMENT_TITLE = 'INFO'

        DOCUMENT_URL   = FILE "'FILE://C:\Users\iPrv\Desktop\Cover Letter.docx'

        NO_FLUSH       = 'X'

        OPEN_INPLACE   = 'X'

      IMPORTING

        ERROR          = CL_ERR.

  ENDIF.

  DATA: IS  TYPE I.

  IF RB_XLS EQ 'X'.

    CALL METHOD CL_DOC->HAS_SPREADSHEET_INTERFACE

      EXPORTING

        NO_FLUSH     = 'X'

      IMPORTING

        IS_AVAILABLE = IS.

    CALL METHOD CL_DOC->GET_SPREADSHEET_INTERFACE

      EXPORTING

        NO_FLUSH        = SPACE

      IMPORTING

        SHEET_INTERFACE = CL_SHT.

  ELSEIF RB_DOC EQ 'X'.

    CALL METHOD CL_DOC->HAS_WORDPROCESSOR_INTERFACE

      EXPORTING

        NO_FLUSH     = 'X'

      IMPORTING

        IS_AVAILABLE = IS.

    CALL METHOD CL_DOC->GET_WORDPROCESSOR_INTERFACE

      EXPORTING

        NO_FLUSH     = SPACE

      IMPORTING

        WP_INTERFACE = CL_WRD.

  ENDIF.

ENDMODULE.                 " STATUS_9000  OUTPUT

*&---------------------------------------------------------------------*

*&      Form  SUB_GET_FILE

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

*  -->  p1        text

*  <--  p2        text

*----------------------------------------------------------------------*

FORM SUB_GET_FILE .

  DATA: LIT_FILE  TYPE FILETABLE,

        LFL_FILE  LIKE LINE OF LIT_FILE,

        L_STR     TYPE C,

        L_RC      TYPE I.

  CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG

    EXPORTING

      WINDOW_TITLE            = 'Open Doc or Xls File'

      FILE_FILTER             = 'Excel File (*.XLSX)|*.XLSX|Document File (*.DOCX)|*.DOCX'

*     WITH_ENCODING           =

*     INITIAL_DIRECTORY       =

*     MULTISELECTION          =

    CHANGING

      FILE_TABLE              = LIT_FILE

      RC                      = L_RC

    EXCEPTIONS

      FILE_OPEN_DIALOG_FAILED = 1

      CNTL_ERROR              = 2

      ERROR_NO_GUI            = 3

      NOT_SUPPORTED_BY_GUI    = 4

      others                  = 5.

  IF SY-SUBRC EQ 0.

    LOOP AT LIT_FILE INTO LFL_FILE.

      P_FILE = LFL_FILE-FILENAME.

    ENDLOOP.

  ENDIF.

ENDFORM.                    " SUB_GET_FILE

Thanks & regards

Praveer Kumar Sen

Labels in this area