Print Program
Unit Overview
The unit introduces the print program. Next, it describes the structure of the print program and how the print program is used for printing. The unit also discusses the various printing options used with text elements and the working of a composer.
Unit Objectives
After completing this unit, you will be able to: • Understand the print program
• Understand the form printing process
Unit Contents
Lesson: Introduction to the Print Program ...120 Lesson: Using the Print Program ...124 Exercise 3: Using the Print Program...135
Lesson: Introduction to the Print Program
Lesson Overview
This lesson introduces you to the print program and the composer. It also discusses the structure of the print program.
Lesson Objectives
After completing this lesson, you will be able to: • Understand the print program
Business Example
You are working with a garment export house that exports a large variety of readymade garments to various distributors across the world. The export house uses SAPscripts to print invoices generated by the Sales and Distribution department.
As a sales executive, you need to print the invoices created by the Sales and Distribution department using a print program. Before doing this, you need to decide how to retrieve the invoice data from the database, define the processing logic, choose a form for printing, select the output device, and set print attributes.
In SAP R/3 ENTERPRISE, both the print program and the form are needed to print documents. The print program is either an ABAP report, type 1 program, or a module pool, Type M Program.
The print program performs the following tasks:
• Retrieves SAP R/3 ENTERPRISE application data from the database
• Defines the form processing logic, which includes the order and frequency of occurrence of text elements
• Chooses a form for printing
• Selects the output device, such as printer, monitor, or fax machine
• Sets various print attributes, such as immediate output, number of copies, and pages to be printed
During the actual print process, the composer controls the format of the document, such as:
• Line and page breaks in the form
• Format of SAPscript control commands and symbol variables • Long text in a document
• The page description for the spool request, i.e. final format or OTF
The Structure of a Print Program
SAP R/3 ENTERPRISE applications, such as FI, CO, and MM, are provided with standard forms and print programs that can be customized.
Within the print program, you must distinguish between the following: • Retrieving data from SAP R/3 ENTERPRISE
• Starting and ending form printing • Changing to different forms • Printing text to the form
Every time you print a form, you must use the paired function modules, OPEN_FORM and CLOSE_FORM. A print program can also print several forms that are either administered individually as separate spool requests or combined into one spool request. To combine forms into one spool request, you can use START_FORM and END_FORM.
To print text elements in form windows, you can use WRITE_FORM. To transfer control commands to a form, you can use CONTROL_FORM.
Lesson Summary
You should now be able to: • Understand the print program
Lesson: Using the Print Program
Lesson Overview
This lesson introduces you to form printing and printing options. It also describes function modules and how a composer works.
Lesson Objectives
After completing this lesson, you will be able to: • Understand the form printing process
Business Example
You are working with a garment export house that exports a large variety of readymade garments to various distributors across the world. The export house uses SAPscripts to create and print invoices generated by the Sales and Distribution department. As a sales executive, you decide to retrieve the invoice data from the database. You define the processing logic, choose a form for printing, select the output device, and set print attributes. Now, you need to print the invoices using the print program.
To specify the form and the desired language, use the FORM and LANGUAGE parameters.
To control the output channel, use the DEVICE parameter with: • PRINTER for print output using spool
• TELEFAX for fax output using spool or SAPconnect
• SCREEN for monitor output using GUI. You cannot print the text if you use this value
To specify attributes, such as the number of copies and immediate output, to print forms or send fax messages, use the OPTIONS parameter (structure ITCPO).
To suppress the dialog box for output device parameters, such as the name of the device in DEVICE = PRINTER or DEVICE = TELEFAX, use the DIALOG parameter. To end form printing, use the CLOSE_FORM function module.
After the form has been printed, you can obtain information and the print and fax parameters from the RESULT parameter (structure ITCPP).
Figure 77: Displaying Several Forms Using One Spool Request
To print several identical forms containing different data in one spool request, begin each form using START_FORM and end the form using END_FORM.
Before using START_FORM for the first time, you must call OPEN_FORM. The function modules for each form are called between START_FORM and END_FORM.
END_FORM does not end the printing process. Use either START_FORM or CLOSE_FORM after END_FORM.
To specify the form under START_FORM, use the FORM and LANGUAGE parameters.
To choose a start page other than the default start page, use the STARTPAGE parameter.
Print control using Function Modules
Figure 78: Output Options for Text Elements
To display text elements in the window of a form, use the function module, WRITE_FORM.
The order in which text elements are printed corresponds to the order in which WRITE_FORM is called. In other words, it is determined by the print program. You must specify the desired text element using the ELEMENT parameter.
The WINDOW parameter specifies the window in which the text element is displayed. The TYPE parameter specifies the output area of the main window.
The FUNCTION parameter specifies whether or not text is to be appended, replaced, or deleted.
Figure 79: Main Windows
You can display text in the main window (type MAIN) in one of three areas: TOP, BODY, or BOTTOM.
The size of the top and bottom areas depends on the size of the text contained in the field. The amount of space needed for this text is deducted from the BODY area. The top and bottom areas of the main window allow you to create headers (titles) and footers. This is similar to creating page headers and footers in ABAP while generating the print list.
You can enter and change the contents of the TOP, BODY, and BOTTOM areas dynamically during form output. SAPscript automatically displays the contents of these areas on every page that contains a main window.
Figure 80: Displaying Text Elements in the Main Window
To print text elements in the main window of a form, use WRITE_FORM. The order in which text elements are printed corresponds to the order in which WRITE_FORM is used in the print program.
The ELEMENT parameter specifies the text element.
The WINDOW parameter must be set to the default value, MAIN. The TYPE parameter must be set to the default value, BODY. The FUNCTION parameter is not relevant here.
After the text elements are displayed in the main window, a page break is triggered when the current page window is full.
You can also use the WRITE_FORM_LINES function module. To do so, go to the LINES parameter and specify the text to be printed.
Figure 81: Creating and Deleting Headings
You can add headers to main windows using WRITE_FORM, TYPE = TOP, FUNCTION = SET.
If no text has been entered in the BODY area, the header is displayed on the current page.
If text has been entered in the BODY area, the header is displayed for the first time at the beginning of the next page.
After a header has been created, the text elements that are to be displayed under the header, for example, a line item from an invoice, are sent to the main window using WRITE_FORM, TYPE = BODY. This output can be several pages long.
After all the text elements have been displayed, the header is deleted by calling WRITE_FORM, TYPE = “TOP”, FUNCTION = “DELETE”.
This deletion first takes place on the following page. If there is text in the BODY, no header can be deleted from the TOP area.
With many application forms, a form of address and short text appears on the first page before the headers are displayed. As a result, in the example above, the header is first displayed in the BODY area and then in the TOP area on subsequent pages.
Figure 82: Adding Text Elements to Non-Main Windows
To add a text element to a non-main window, use WRITE_FORM with FUNCTION = “APPEND”.
Unlike text inserted into the main window, any text that does not fit into the current page window is lost.
As with the main window, the order in which text elements are printed corresponds to the order in which WRITE_FORM is used in the print program.
You must specify the name of the text element in the ELEMENT parameter. You must specify the name of the non-main window in the WINDOW parameter. The TYPE parameter is not relevant here.
Figure 83: Replacing Text Elements in Non-Main Windows
To replace the content of a non-main window with a text element, use WRITE_FORM with FUNCTION = “SET”.
You must specify the name of the text element in the ELEMENT parameter. You must specify the name of the window in the WINDOW parameter. The TYPE parameter is not relevant here.
You must specify the default value, “SET”, in the FUNCTION parameter.
Figure 84: Deleting a Text Element in a Non-Main Window
You must specify the text element in the ELEMENT parameter. You must specify the window in the WINDOW parameter. The TYPE parameter is not relevant here.
“DELETE”, in the FUNCTION parameter.
Working of Composer
Figure 85: How the Composer Works
The main window controls the page break. The text elements of non-main windows are not processed until a page break.
• A page break can be triggered by: – An overflow of the main window
– A NEW-PAGE command in the main window
The main window of a form is divided into the TOP, BODY, and BOTTOM areas. To print text elements in the TOP, BODY, or BOTTOM area of the main window or in non-main windows, use WRITE_FORM.
Figure 86: Calling Commands Using a Program
The function module, CONTROL_FORM, allows you to create SAPscript control statements from an ABAP program.
Enter the SAPscript statement you want to use to control output in the COMMAND parameter of the function module without the /: addition.
In the example above, text element, ITEM_LINE, is page protected. As a result, the contents of text element, ITEM_LINE, are always displayed in one block on one page. You need to execute the SAPscript statement, PROTECT ... ENDPROTECT, in SAPscript (See the unit on Symbols and Control Commands).
Exercise 3: Using the Print Program
Exercise Objectives
After completing this exercise, you will be able to: • Call function modules for form printing • Control printing using print programs • Enhance print programs
• Print preview using the print program
Business Example
In most cases, standard forms are altered to reflect the needs of individual customers. In most cases, the print program must also be altered to reflect these needs.
Task 1:
Object Name / Description
Name of the form to be used SAPBC460D_FM_03 Name of the print program to be created Z_BC460_EX5A_##
Development class $TMP
Note: ## is your group number
Write a print program that outputs form SAPBC460D_FM_03. Take a look at the form in the Form Painter first so that you know in which sequence the print program should display the form’s text elements.
Task 2:
Allow users to determine which page printout should begin by including a selection screen on which they can enter a page number.
Note: Choose Pattern to adopt function modules.
Object Name / Description
Name of the form to be created Z_BC460_EX5_## Name of the form to be used SAPBC460D_FM_05 Name of the print program to be used as
a template
SAPBC460T_05 Name of the print program to be created Z_BC460_EX5_##
Development class $TMP
Note: ## is your group number 1.
Task 3:
Copy print program SAPBC460T_05 and complete the report by inserting function module calls that open, start, and fill the form with data.
1. Copy program SAPBC460T_05.
Select Tools → ABAP Workbench → Development → ABAP Editor. Enter the program name SAPBC460T_05 and select Program → Copy. Enter Z_BC460_EX5_## and choose Copy to copy the source code and text elements.
Choose the Local object function from the Create object catalog entry window. Edit the source code of report Z_BC460_EX5_##. This report contains a print program whose function module calls for the SAPscript form output marked by asterisks. Complete the report by inserting function modules calls. The sections where code must be inserted are marked as follows:
*CALL FUNCTION
****************************************
Note: Choose Pattern to adopt function modules.
If you have problems determining the proper sequence of the function calls, consult the training documentation for more information about the basic structure of print programs. It may also be useful to check the MAIN window of form SAPBC460D_FM_05. Such a check will show you the order of text elements.
Test your finished program.
Run your program by selecting Program → Execute. The FORM parameter has to be provided the name of the SAPscript form used for printing.
Do not change the value SAPBC460D_FM_05 suggested in this example.
To check the form output, select Print preview. If you have a printer, you can even print the form.
Task 4:
Create a new form with the name Z_BC460_EX5_## that is to be used by your program.
1. Copy form SAPBC460D_FM_05 by selecting Form → Copy (see the exercises on the PC editor and the line editor).
Task 5:
Use Program symbols to add your program to the print program list. 1. Select Include → Symbols → Program symbols.
Choose Add print program.
Task 6:
Add a field to form Z_BC460_EX5_## for the destination city (text element ITEM_LINE).
1. Add a tab and the destination city to the end of the line item. Define a new tab position to position the field. Insert the program symbol for the destination city from table SPFLI.
If not enough space is left at the end of the line item, reduce the output length using formatting options.
Add the new column to the text elements for item headers (ITEM_HEADER) as well.
Activate the form and execute the print program Z_BC460_EX5_## with your new form.
Note: Merely going back to your selection screen is not enough.
Task 7:
If your destination is NEW YORK, display the following message in bold font style after the line item:
”Please be aware of delayed check-in times in New York due to an American air traffic controllers strike!”
1. Print this text in a separate line after the line item. Use the command IF ... ELSE ... ENDIF.
In order to prevent a page break from occurring between the line item and the message, use the commands PROTECT ... ENDPROTECT.
Task 8:
Add country names to customer addresses if the country of the sender differs from that of the customer.
1. Add COUNTRY as a subcommand to the ADDRESS control command in the ADDRESS window. Insert the program symbol for the customer’s country after COUNTRY. The corresponding program symbol can be found in table SCUSTOM.
When you test this form, the address output should appear in one of two different ways. If the customer’s country is DE, then “Germany” should appear in the last line of the address. If the customer’s country is US, then no country should appear in the last line of the address.
Solution 3: Using the Print Program
Task 1:
Object Name / Description
Name of the form to be used SAPBC460D_FM_03 Name of the print program to be created Z_BC460_EX5A_##
Development class $TMP
Note: ## is your group number
Write a print program that outputs form SAPBC460D_FM_03. Take a look at the form in the Form Painter first so that you know in which sequence the print program should display the form’s text elements.
1.
a) The solution program for the first exercise is called SAPBC460S_05A.
*&---*
*& Report SAPBC460S_05A * *---*
& Example print program for training course BC460, & unit 5 *
&---* REPORT sapbc460s_05a .
PARAMETERS: stpage(10) DEFAULT ’FIRST’.
---* * Open form printing
CALL FUNCTION ’OPEN_FORM’ EXPORTING
form = ’SAPBC460D_FM_03’ EXCEPTIONS
OTHERS = 1. IF sy-subrc <> 0.
WRITE: ’Error in OPEN_FORM’(001). ENDIF.
* Select start page CALL FUNCTION ’START_FORM’
OTHERS = 1. IF sy-subrc <> 0.
WRITE: / ’Error in START_FORM’(002). ENDIF.
* Write text element INTRODUCTION CALL FUNCTION ’WRITE_FORM’
EXPORTING
element = ’INTRODUCTION ’ EXCEPTIONS
OTHERS = 1. IF sy-subrc <> 0.
WRITE: / ’Error in WRITE_FORM, element INTRODUCTION’(003). ENDIF.
* Write text element ITEMS CALL FUNCTION ’WRITE_FORM’
EXPORTING
element = ’ITEMS’ EXCEPTIONS
OTHERS = 1. IF sy-subrc <> 0.
WRITE: / ’Error in WRITE_FORM, element ITEMS’(004). ENDIF.
* Write text element CLOSING_REMARK CALL FUNCTION ’WRITE_FORM’
EXPORTING
element = ’CLOSING_REMARK’ EXCEPTIONS
OTHERS = 1. IF sy-subrc <> 0.
WRITE: / ’Error in WRITE_FORM, element CLOSING_REMARK’(005). ENDIF.
* End form printing CALL FUNCTION ’END_FORM’
EXCEPTIONS
unopened = 1 bad_pageformat_for_print = 2 OTHERS = 3. IF sy-subrc <> 0.
WRITE: / ’Error in END_FORM’(006). ENDIF.
CALL FUNCTION ’CLOSE_FORM’ EXCEPTIONS unopened = 1 bad_pageformat_for_print = 2 OTHERS = 3. IF sy-subrc <> 0.
WRITE: / ’Error in CLOSE_FORM’(007). ENDIF.
Task 2:
Allow users to determine which page printout should begin by including a selection screen on which they can enter a page number.
Note: Choose Pattern to adopt function modules.
Object Name / Description
Name of the form to be created Z_BC460_EX5_## Name of the form to be used SAPBC460D_FM_05 Name of the print program to be used as
a template
SAPBC460T_05 Name of the print program to be created Z_BC460_EX5_##
Development class $TMP
Note: ## is your group number 1.
a) The solution program of the second exercise is called SAPBC460S_05B. Any program lines added are highlighted.
*&---*
*& Report SAPBC460S_05B * *&---*
*& Example print program for training course BC460, & unit 5
*&---* REPORT sapbc460s_05b .
DATA customers LIKE scustom OCCURS 100 WITH HEADER LINE.
DATA bookings LIKE sbook OCCURS 1000