A power SAP Scirpt program to print customer address details for Postage purposes , generally which requires at the time of sending annual greetings/ news to customers in mass or bulk.
Below is the procedure for sample program to achieve this one.
1. Open Sap Script Tcode SE71. Create new scirpt.
2. Add one Main window and save it.
3. Go to page windows and add main window 12 times as below. You can add it graphically (settings->Formpainter->Checkmark the Graphical Form painter).
4. Select a PAGE Window and click on text elements (F9) to enter the our variable names here to print the data. In this case we are printing the Customer address data from Internal tables.
Then Activate the script.
5. Below is the report program code to fetch the customer address detail from database tables to internal tables and input that data to SAP Script to get the output. The code is as follows.
*&---------------------------------------------------------------------*
*& Include ZR_C101_CSD106_INC1
*&---------------------------------------------------------------------*
REPORT ZR_C101_CSD106_1 MESSAGE-ID ZCSD106.
DATA: BEGIN OF IT_KNB1 OCCURS 0,
KUNNR TYPE KNB1-KUNNR,
BUKRS TYPE KNB1-BUKRS,
END OF IT_KNB1.
DATA: BEGIN OF IT_KNA1 OCCURS 0,
KUNNR TYPE KNA1-KUNNR,
LAND1 TYPE KNA1-LAND1,
NAME1 TYPE KNA1-NAME1,
ORT01 TYPE KNA1-ORT01,
PSTLZ TYPE KNA1-PSTLZ,
REGIO TYPE KNA1-REGIO,
STRAS TYPE KNA1-STRAS,
TELF1 TYPE KNA1-TELF1,
END OF IT_KNA1.
DATA: BEGIN OF IT_T005T OCCURS 0,
LAND1 TYPE T005T-LAND1,
LANDX TYPE T005T-LANDX,
END OF IT_T005T.
DATA: BEGIN OF IT_T005U OCCURS 0,
LAND1 TYPE T005U-LAND1,
REGIO TYPE T005U-BLAND,
BEZEI TYPE T005U-BEZEI,
END OF IT_T005U.
** Select options declarations.
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-SS1.
SELECT-OPTIONS S_BUKRS FOR IT_KNB1-BUKRS NO INTERVALS NO-EXTENSION.
SELECTION-SCREEN END OF BLOCK B1.
START-OF-SELECTION.
*** Fetch the data from Database tables to internal tables based on the Company code entered in the Selection Screen.
SELECT KUNNR
BUKRS INTO TABLE IT_KNB1
FROM KNB1
WHERE BUKRS IN S_BUKRS.
IF NOT IT_KNB1[] IS INITIAL.
SORT IT_KNB1 BY KUNNR.
SELECT KUNNR
LAND1
NAME1
ORT01
PSTLZ
REGIO
STRAS
TELF1 INTO TABLE IT_KNA1
FROM KNA1
FOR ALL ENTRIES IN IT_KNB1
WHERE KUNNR = IT_KNB1-KUNNR.
SORT IT_KNA1 BY LAND1.
SELECT LAND1
LANDX INTO TABLE IT_T005T
FROM T005T
FOR ALL ENTRIES IN IT_KNA1
WHERE LAND1 = IT_KNA1-LAND1
AND SPRAS = SY-LANGU.
SELECT LAND1
BLAND
BEZEI INTO TABLE IT_T005U
FROM T005U
FOR ALL ENTRIES IN IT_KNA1
WHERE LAND1 = IT_KNA1-LAND1
AND BLAND = IT_KNA1-REGIO
AND SPRAS = SY-LANGU.
ELSE.
FORMAT COLOR 3.
WRITE : 'Invalid Company code selected'.
ENDIF.
END-OF-SELECTION.
** Sorting internal tables.
SORT: IT_KNA1 BY KUNNR,
IT_T005T BY LAND1,
IT_T005U BY LAND1 REGIO.
** Opening the form ( Open SAP Script form which we created above)
CALL FUNCTION 'OPEN_FORM'
EXPORTING
FORM = 'ZC101_CSD106'
LANGUAGE = SY-LANGU.
** Start looping for printing on to the page windows.
LOOP AT IT_KNA1.
READ TABLE IT_T005T WITH KEY LAND1 = IT_KNA1-LAND1 BINARY SEARCH.
READ TABLE IT_T005U WITH KEY LAND1 = IT_KNA1-LAND1 REGIO = IT_KNA1-REGIO.
CALL FUNCTION 'WRITE_FORM'
EXPORTING
ELEMENT = 'E1'
FUNCTION = 'SET'
TYPE = 'BODY'
WINDOW = 'MAIN'.
CLEAR: IT_KNA1,
IT_T005T,
IT_T005U.
ENDLOOP.
** Closing the form.
CALL FUNCTION 'CLOSE_FORM'.
And the finally output will be as below.