It takes a lot of effort to enter names and other relevant information into form letters. Create a custom program that allows you to download SAP R/3 data into Microsoft Word document templates to expedite this process.
Key Concept
Object Linking and Embedding (OLE) is a technology that allows you to create objects in an application and use them with other applications via embedded or linked objects (e.g., SAP and Microsoft Word, Oracle and Excel). You can integrate desktop applications that provide their functionality into R/3 via a specific version of OLE, an OLE2 automation server such as Excel or Word. ABAP supports the OLE2 automation technique through its open object interface. From R/3 3.0, SAP has provided a set of ABAP statements for OLE automation that enable an ABAP program to communicate with desktop applications. SAP R/3 uses OLE to send R/3 data to Microsoft Word. For more information on SAP OLE automation, refer to the SAP documentation at
https://help.sap.com/saphelp_47x200/helpdata/en/59/ ae3cac488f11d189490000e829fbbd/frameset.htm.
In the article “Create Microsoft Word Form Letters Using SAP ABAP Query” you learn how to execute an SAP query and download SAP data into a Word template. That functionality requires the user to create the template or to find the correct template to load the data into. By contrast, the functionality identified in this article automatically calls up the correct template and directly downloads the data.
This type of functionality includes the ability to attach the Word document to an Outlook or Lotus Notes email message and send the document to another user. In addition, Word documents allow you to have colored fonts and images. You can store a Word template in R/3 and centralize the template by adding a simple step to access the download program. If you ensure that everyone is using the same Word template, you can allow multiple individuals to run it.
Using the example of company XYZ Inc., we show you why you might want to download SAP R/3 data directly into a Word document. We also provide you with some sample code to help you build this download program. It was written for SAP R/3 4.6B and above. We used this method for a client program that generates and fills a popular HR form in Word from SAP.
Compared to other methods such as SAPscript and SmartForms, we prefer the approach explained in this article because it is simple. SAPscript is time consuming, and some people are unfamiliar with SmartForms. Most people are comfortable using Word, so the learning curve is shorter. When the data is in Word, people can email it to anyone, and Word templates are easy to change and maintain.
Service Anniversary Letters
XYZ Inc. sends service anniversary letters to certain employees. Let’s examine the six steps that the company must follow to send a service anniversary letter to an employee:
Step 1. Run a service anniversary report. Every month, Joan in the centralized Human Resources department at XYZ Inc. runs a service anniversary report in SAP. This report identifies employees who have reached their first, fifth, 10th, or 15th year of service.
Step 2. Extract SAP data into Word. Once Joan extracts this data from SAP, she must type in each employee’s name and the number of service years into a Word document.
Step 3. Joan from the HR department sends the Word document to each employee’s manager.
Step 4. The employee’s manager prints the document, signs it, and takes it to the department’s director or vice president.
Step 5. The department’s director or vice president signs the service anniversary letter and returns it to the employee’s manager.
Step 6. The employee’s manager gives the letter to the employee.
Now let’s look at this process once the HR department employees are able to download the SAP data directly into a Word document.
Step 1. Run the report. XYZ Inc. gives managers the ability to run the report identifying employees who have reached their first, fifth, 10th, or 15th year of service. Joan in HR is not involved in the process.
Step 2. The report automatically merges the SAP R/3 data directly into a Word document. The manager who prints the document, signs it, and takes it to the department’s director or vice president. The manager (or whoever runs the report) can print, save, or send the document via email.
Step 3. The department’s director or vice president signs the service anniversary letter and returns it to the employee’s manager.
Step 4. The employee’s manager gives the letter to the employee.
Create the Template
To create the program used in step 2 of the second scenario above, create a Microsoft Word template. Your SAP R/3 download program calls this template, and the data from the program will be automatically merged into the Word document. To create the Word document template, a member of your technical or functional team needs to open Word and create a new document. If desired, include the company letter header or logo at the top of the document.
Type the letter. Define merge fields as placeholders for the data that will be merged from the SAP R/3 system. The merge are indicated by using the following symbols: << to begin the tag label and >> to end the tag label (Figure 1). Make certain that the tag labels in the Word template match the field names from the SAP R/3 program.

Figure 1
Sample Word template
The sample code provided in the download expects the Word document to be stored on the users’ C drive in Program FilesSAP. For purposes of this example, be sure you save the Word document on the C drive in a directory called Program FilesSAP.
Note
Please note that our example is for test purposes only. In a real situation, you would create everything in your test environment, then move it over to production when ready.
In our example, you will place the Word template on the users’ C drives in C:Program FilesSAP. However, you have additional options that you could explore if you were doing this for a production environment. Your company may want to place the Word document on either the SAP R/3 Unix or DB2 directory or a shared drive directory on the LAN instead of a user’s C drive. The reason you would not want to tell users to store this document on their C drive is that they might not have most recent template if the program were ever to change. The downside is that if you put the template on the LAN drive, someone might delete or change the Word document without realizing that the SAP download program uses it. To avoid this confusion, store the template in a Unix or DB2 directory.
Create the SAP R/3 Download Program
A member of your technical team must create the SAP R/3 program that retrieves the service anniversary information and include the code to call Microsoft Word and merge the SAP data into the Word template (Figure 1). You can download a sample program at the bottom of the article. Here are the steps for implementing this code:
Note
To connect the program output to the Word template, use the call method of wordobj “MailMergeOpenDataSource” and the call method of wordobj “MailMergeToDoc” (ABAP commands for OLE) in the ABAP program demonstrated in this article. This program assumes that the template file in located in PC directory C:Program FilesSAP.
No extra Basis steps are required to make this functionality work.
Step 1. Create a new ABAP program. Log into SAP R/3 and go to ABAP Editor via transaction SE80. Create a new program called zzjf_word_46up
(Figure 2). Do not include a top include; in other words, do not check the With TOP INCL. check box (Figure 3).

Figure 2
Create an SAP program

Figure 3
Do not include a top include
Give the program the following attributes (Figure 4):
- Type: Executable program
- Status: Test program
- Application: Human resources
- Logical database: PNP (By associating the logical database PNP to the program, the standard PNP logical database selection screen automatically appears, as shown in Figure 5. You do not have to enter anything into this screen.)

Figure 4
List of program attributes

Figure 5
Standard PNP logical database selection screen
Normally you would not save this as a local program because local programs can not be promoted into production. Since this is a sample program and we do not want this to go into production as is, we save this as a local program to ensure that we do not create a transport that could accidentally be sent to a production environment (Figure 6).

Figure 6
Save as a local program
Step 2. Copy the sample program. Paste the copied program logic into the ABAP Editor (Figure 7). The logic within the code will automatically give you one more parameter in the selection screen of Figure 5. It is called Years of Service >= at the bottom of Figure 5 (technical name P_YEAR). This represents the number of years of service you wish to select. To test this program, you need to use existing test data or hire an employee.

Figure 7
Insert the code in ABAP Editor
Step 3. Ensure that the user has the proper authorizations to run the SAP R/3 service anniversary report.
Step 4. Execute the report.
Jim Fang
Jim Fang is a techo-functional SAP HCM consultant with over 11 years experience. He has been a key player for numerous major SAP HR/Payroll and e-Recruiting projects. He has extensive experience with HCM business processes, payroll schema, taxations for US/UK/Canada, BSP, Web Dynpro, and HCM Processes and Forms. Currently he is a partner and senior consultant with ReachNett Consulting.
You may contact the author at jim.fang@reachnett.com.
If you have comments about this article or publication, or would like to submit an article idea, please contact the editor.
Dawn Burns
Dawn Burns is an SAP-certified human resources senior consultant and Quality Assurance Manager and HR Consultant with Howrey LLP. She is a former SAP Human Resources instructor for SAP America and has more than 12 years of experience in human resources and information technology.
If you have comments about this article or publication, or would like to submit an article idea, please contact the editor.