Make your own free website on Tripod.com
 

SAP ABAP Programming, Functional and Basis Components Books


Call Microsoft Word from ABAP

4.6x

* Let user type in the text file with Microsoft Word
* Use WS_EXECUTE to activate Microsoft Word from ABAP
* After saving, exit from Microsoft Word and upload the files to
* Internal Table for printing.

REPORT ZWSEXECUTE.

DATA: BEGIN OF ITAB OCCURS 3,
      LINE(50),
      END OF ITAB.

PARAMETERS: PROG(70) DEFAULT
            'C:\Program Files\Microsoft Office\Office\WINWORD.EXE'.
PARAMETERS: FILE1(70) DEFAULT 'C:\TEMP\TEST.TXT'.
* Tick to print the Text file after saving from MS WORDS
PARAMETERS: S_UP  AS CHECKBOX.
* Tick to create new or overwrite Text file
PARAMETERS: S_NEW AS CHECKBOX.

IF S_UP = 'X'.
   CALL FUNCTION 'GUI_UPLOAD'
        EXPORTING
             FILENAME        = 'FILE1'
        TABLES
             DATA_TAB        = ITAB
        EXCEPTIONS
             FILE_OPEN_ERROR = 1.

   IF SY-SUBRC = 0.
     LOOP AT ITAB.
       WRITE: / ITAB.
     ENDLOOP.
   ELSE.
     WRITE: / 'File open error.'.
   ENDIF.
ELSE.
IF S_NEW = 'X'.
   CALL FUNCTION 'GUI_DOWNLOAD'
        EXPORTING
             FILENAME = 'FILE1'
        TABLES
             DATA_TAB = ITAB
        EXCEPTIONS
             FILE_WRITE_ERROR              = 1
             NO_BATCH                      = 2
             GUI_REFUSE_FILETRANSFER       = 3
             INVALID_TYPE                  = 4
             OTHERS                        = 5.
ENDIF.
CASE SY-SUBRC.
     WHEN 1.
          WRITE: / 'GUI DOWNLOAD FILE WRITE ERROR'.
     WHEN 2.
          WRITE: / 'GUI DOWNLOAD NO BATCH'.
     WHEN 3.
          WRITE: / 'GUI DOWNLOAD GUI REFUSE FILETRANSFER'.
     WHEN 4.
          WRITE: / 'GUI DOWNLOAD INVALID TYPE'.
     WHEN 5.
          WRITE: / 'GUI DOWNLOAD OTHERS'.
ENDCASE.

CALL FUNCTION 'WS_EXECUTE'
  EXPORTING
     PROGRAM       = PROG
     COMMANDLINE   = 'FILE1'
     INFORM        = ' '
  EXCEPTIONS
     FRONTEND_ERROR           = 1
     NO_BATCH                 = 2
     PROG_NOT_FOUND           = 3
     ILLEGAL_OPTION           = 4
     GUI_REFUSE_EXECUTE       = 5
     OTHERS                   = 6.

CASE SY-SUBRC.
   WHEN 1.
     WRITE: / 'FRONTEND ERROR'.
   WHEN 2.
     WRITE: / 'NO BATCH'.
   WHEN 3.
     WRITE: / 'PROGRAM NOT FOUND'.
   WHEN 4.
     WRITE: / 'ILLEGA OPTION'.
   WHEN 5.
     WRITE: / 'GUI REFUSE EXECUTE'.
   WHEN 6.
     WRITE: / 'OTHERS'.
ENDCASE.
ENDIF.

Back to Previous

Return to SAP Hints and Tips on Configuration and ABAP/4 Programming