
In this blog, we’ll explore how to use OLE objects in SAP ABAP to automate tasks in Microsoft Word. We will cover the basics, including how to create an OLE object, manipulate data, and handle errors.
Introduction to OLE in SAP ABAP
OLE Automation in ABAP enables you to control Microsoft Office applications. You can create documents, fill in data, format cells, and even run macros—all from within your ABAP programs. This is particularly useful for generating reports, exporting data, and performing complex calculations.
Prerequisites
Before you begin, ensure that:
You have access to a Microsoft Office installation on the machine running the SAP GUI.
You have basic knowledge of ABAP programming.
Creating an OLE Object
To start using OLE in ABAP, you first need to create an OLE object. The CREATE OBJECT statement in ABAP is used for this purpose.
REPORT ZTEST_BLOGYS.
PARAMETERS: p_file TYPE localfile DEFAULT 'C:\Users\Yeshica.Sharma\Downloads\p0.docx'. “we are declaring file pathwhere we need to store word file.
INCLUDE ole2incl.
DATA: “we are declaring data for ole object
*** OLE Object
gs_word TYPE ole2_object, "Word Object
gs_documents TYPE ole2_object, "List of documents
gs_document TYPE ole2_object, "Current / Active document
gs_selection TYPE ole2_object, "Current Cursor Selection
gs_actdoc TYPE ole2_object , "Active document
gs_font TYPE ole2_object , "Font
gs_parformat TYPE ole2_object , "Paragraph format
gs_tables TYPE ole2_object , "Tables
gs_table TYPE ole2_object,
gs_range TYPE ole2_object,
gs_cell TYPE ole2_object,
gs_border TYPE ole2_object,
gs_interior TYPE ole2_object,
gs_application TYPE ole2_object, "Application
gv_pos(5) TYPE n , "Position information for table
wf_lines TYPE i,
wf_counter TYPE i,
lv_date TYPE char15,
lv_data TYPE string,
lv_copyrightdata TYPE string.
"Create word object
CREATE OBJECT gs_word 'WORD.APPLICATION'.
* Setting object's visibility property
SET PROPERTY OF gs_word 'Visible' = 1.
* Opening a new document
CALL METHOD OF gs_word 'Documents' = gs_documents.
CALL METHOD OF gs_documents 'Add' = gs_document.
* Activating the sheet
CALL METHOD OF gs_document 'Activate'.
* Getting active document handle
GET PROPERTY OF gs_word 'ActiveDocument' = gs_actdoc.
* Getting applications handle
GET PROPERTY OF gs_actdoc 'Application' = gs_application.
* Getting handle for the selection which is here the character at the cursor position
GET PROPERTY OF gs_application 'Selection' = gs_selection.
GET PROPERTY OF gs_selection 'Font' = gs_font.
GET PROPERTY OF gs_selection 'ParagraphFormat' = gs_parformat.
Types : Begin of ty_mara,
Matnr type mara-matnr,
ersda type mara-ersda,
ernam type mara-ernam,
end of ty_mara.
data : it_mara type table of mara,
wa_mara type mara.
select matnr ersda ernam from mara into corresponding fields of table it_mara UP TO 5 ROWS.
CALL METHOD OF gs_selection 'TypeText'
EXPORTING
#1 = 'DATA OF MARA TABLE'.
GET PROPERTY OF gs_actdoc 'Tables' = gs_tables.
GET PROPERTY OF gs_selection 'Range' = gs_range.
DESCRIBE TABLE it_mara LINES wf_lines.
wf_lines = wf_lines + 1.
IF sy-subrc NE 0.
ENDIF.
CALL METHOD OF gs_tables 'Add' = gs_table
EXPORTING #1 = gs_range
#2 = wf_lines
#3 = '3'. "Columns
GET PROPERTY OF gs_table 'Borders' = gs_border.
SET PROPERTY OF gs_border 'Enable' = '1'.
IF sy-subrc NE 0.
ENDIF.
CALL METHOD OF gs_table 'Cell' = gs_cell
EXPORTING #1 = '1'
#2 = '1'.
* *setting the bg color
CALL METHOD OF gs_cell 'Shading' = gs_interior.
SET PROPERTY OF gs_interior 'BackgroundPatternColor' = -570376193.
GET PROPERTY OF gs_cell 'Range' = gs_range.
SET PROPERTY OF gs_range 'Text' = 'matnr'.
CALL METHOD OF gs_range 'Font' = gs_font.
SET PROPERTY OF gs_font 'Bold' = '1'. " Make the text bold
CALL METHOD OF gs_range 'SetFont' = gs_font.
IF sy-subrc NE 0. ENDIF.
************************* Col 2
CALL METHOD OF gs_table 'Cell' = gs_cell
EXPORTING #1 = '1'
#2 = '2'.
* *setting the bg color
CALL METHOD OF gs_cell 'Shading' = gs_interior.
SET PROPERTY OF gs_interior 'BackgroundPatternColor' = -570376193.
GET PROPERTY OF gs_cell 'Range' = gs_range.
SET PROPERTY OF gs_range 'Text' = 'ersda'.
CALL METHOD OF gs_range 'Font' = gs_font.
SET PROPERTY OF gs_font 'Bold' = '1'. " Make the text bold
CALL METHOD OF gs_range 'SetFont' = gs_font.
IF sy-subrc NE 0. ENDIF.
********************** Col 3
CALL METHOD OF gs_table 'Cell' = gs_cell
EXPORTING #1 = '1'
#2 = '3'.
* *setting the bg color
CALL METHOD OF gs_cell 'Shading' = gs_interior.
SET PROPERTY OF gs_interior 'BackgroundPatternColor' = -570376193.
GET PROPERTY OF gs_cell 'Range' = gs_range.
SET PROPERTY OF gs_range 'Text' = 'ernam'.
CALL METHOD OF gs_range 'Font' = gs_font.
SET PROPERTY OF gs_font 'Bold' = '1'. " Make the text bold
CALL METHOD OF gs_range 'SetFont' = gs_font.
IF sy-subrc NE 0.
ENDIF.
wf_counter = 1.
LOOP AT it_mara INTO wa_mara.
wf_counter = wf_counter + 1.
CALL METHOD OF gs_table 'Cell' = gs_cell
EXPORTING #1 = wf_counter
#2 = '1'.
GET PROPERTY OF gs_cell 'Range' = gs_range.
SET PROPERTY OF gs_range 'Text' = wa_mara-matnr.
IF sy-subrc NE 0.
ENDIF.
CALL METHOD OF gs_table 'Cell' = gs_cell
EXPORTING #1 = wf_counter
#2 = '2'.
GET PROPERTY OF gs_cell 'Range' = gs_range.
SET PROPERTY OF gs_range 'Text' = wa_mara-ersda.
IF sy-subrc NE 0.
ENDIF.
CALL METHOD OF gs_table 'Cell' = gs_cell
EXPORTING #1 = wf_counter
#2 = '3'.
GET PROPERTY OF gs_cell 'Range' = gs_range.
SET PROPERTY OF gs_range 'Text' = wa_mara-ernam.
IF sy-subrc NE 0.
ENDIF.
ENDLOOP.
*--Advancing the cursor to the end of the table
GET PROPERTY OF gs_table 'Range' = gs_range .
GET PROPERTY OF gs_range 'End' = gv_pos .
SET PROPERTY OF gs_range 'Start' = gv_pos .
CALL METHOD OF gs_range 'Select' .
DO 2 TIMES.
CALL METHOD OF gs_selection 'TypeParagraph' .
ENDDO.
IF sy-subrc NE 0.
ENDIF.
Output is like below -
Conclusion
Integrating OLE objects in SAP ABAP provides a seamless way to leverage the powerful functionalities of Microsoft Office applications directly within your SAP environment. By automating tasks in Excel, Word, or PowerPoint, you can enhance productivity, streamline workflows, and create dynamic reports. Understanding how to create and manipulate OLE objects in ABAP opens up a myriad of possibilities for SAP developers, allowing for the creation of sophisticated solutions that integrate the best of SAP and Microsoft Office capabilities. With the steps and examples provided, you are now equipped to start incorporating OLE automation into your ABAP programs, making your SAP applications more versatile and efficient.
Hope this may helpful.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
6 | |
6 | |
5 | |
5 | |
4 | |
3 | |
3 | |
3 | |
3 | |
3 |