SAP Scripts - Print Program


Advertisements

In SAP Scripts, Print Program is used to print the actual form and get the data from database tables, choosing a form and print the text elements in a defined format. It retrieves the data from the database and combines it with the user input, formats the data and prints it.

All print programs and forms are stored in table TNAPR.

Different functional modules are used in Print Program. To start a print program, OPEN_FORM functional module is used, and to end the program CLOSE_FORM functional module is used.

OPEN_FORM − In Print Program, this function should be called first before any printing can take place. You specify the name of the form and the print language.

CALL FUNCTION 'OPEN_FORM' 

START_FORM − This function is called to use different forms with similar characteristics in a single request.

CALL FUNCTION ’START_FORM’

WRITE_FORM − This function is used to write text in a window in the form using text elements.

CALL FUNCTION ‘WRITE_FORM’

CONTROL_FORM − This function is used to insert SAP Script control commands in an ABAP program.

CALL FUNCTION ‘CONTROL_FORM’

END_FORM − This function is called in the end and it has no exporting parameters.

CALL FUNCTION ‘END_FORM’ 

CLOSE_FORM − To view the standard form and the standard Print program, run Transaction Code: NACE

Enter Application Types and click the Output Type at the top.

NACE

Output Types

In the following screenshot, you can see the Application name for the selected service.

Partner Functions

Print Program − Example

Following is a sample print program that creates an invoice with company-related information such as customer data, date, flight booking, etc.

Section 1 - Get customer data

TABLES: zcustom, zbook, zpfli.  
DATA: bookings like zbook... 
select * from... 
/In this section, you are reading the data from tables in database./ 

Section 2 - Open form

CALL FUNCTION 'OPEN_FORM'  
EXPORTING  
DEVICE     = 'PRINTER'  
FORM       = 'EXAMPLE1'  
DIALOG     = 'X'  
OPTIONS    = 
EXCEPTIONS  
CANCELLED  = 1 
DEVICE     = 2 
FORM       = 3 
OTHERS     = 11 
/In this section, you are calling OPEN_FORM function module to initialize print output./

In the above function module, the parameter −

  • FORM shows the name of the form.

  • DEVICE can be PRINTER (print using spool), TELEFAX (fax output) or a SCREEN (output to screen)

  • OPTIONS shows a structure of type ITCPO to control the various attributes - Print preview, number of copies.

Section 3 - Print table heading

CALL FUNCTION 'WRITE_FORM' 
EXPORTING 
ELEMENT  = 'textelement’  
TYPE     = 'TOP' 
WINDOW   = 'MAIN' 
FUNCTION = 'SET' 
... 
/In this section, you use WRITE_FORM function to output general text elements and column 
heading/
  • ELEMENT function module shows the ‘textelement’ to be printed and ‘WINDOW’ shows which window of the form to be printed.

  • TYPE shows the output area of the window like- TOP, BOTTOM, or BODY.

  • FUNCTION tells the text to be replaced, added or appended.

Section 4 - Print customer bookings

LOOP AT bookings WHERE  
CALL FUNCTION 'WRITE_FORM'  
EXPORTING  
ELEMENT  = 'BOOKING'  
TYPE     = 'BODY'  
WINDOW   = 'MAIN'  
...  
ENDLOOP  
/In this section, text element BOOKING is used to output the bookings of a customer from
the loop from BOOKING table in database./

Section 5 - Close form

CALL FUNCTION 'CLOSE_FORM'  
IMPORTING 
* RESULT    = 
EXCEPTIONS 
UNOPENED    = 1 
OTHERS      = 5 
/To end the Print Program/ 

You call this function module in the end and it has no exporting parameter.

Advertisements