REPORT Z_OLE_AUTOMATION_WORD.
***********************************************************************
* This is a simple program that can be used as a reference for OLE
* automation using MS-Word.
* The program will download all users to a Word document using some
* formatting techniques (bold and center).
*
* Program is tested on SAP Release 46B till Netweaver based systems.
***********************************************************************
INCLUDE ole2incl.
TABLES: v_username.
DATA: w_word TYPE ole2_object,
w_document TYPE ole2_object,
w_selection TYPE ole2_object,
w_font TYPE ole2_object,
w_paragraphformat TYPE ole2_object,
w_system(10),
w_line(80).
DATA: BEGIN OF itab OCCURS 0.
INCLUDE STRUCTURE v_username.
DATA: END OF itab.
START-OF-SELECTION.
REFRESH itab.
SELECT * FROM v_username INTO TABLE itab ORDER BY bname.
* Create OLE automation object of type Word.Application.
CREATE OBJECT w_word 'Word.Application'.
* Display MS-Word when executing the program.
SET PROPERTY OF w_word 'Visible' = 1.
* Add a new blank document.
CALL METHOD OF w_word 'Documents' = w_document.
CALL METHOD OF w_document 'Add'.
* Add data to the new MS-Word document.
CALL METHOD OF w_word 'Selection' = w_selection.
CALL METHOD OF w_selection 'ParagraphFormat' = w_paragraphformat.
CALL METHOD OF w_selection 'Font' = w_font.
* Use bold, a bigger font and center the title.
SET PROPERTY OF w_paragraphformat 'Alignment' = 1.
SET PROPERTY OF w_font 'Size' = 16.
SET PROPERTY OF w_font 'Bold' = 1.
CONCATENATE syst-sysid '(' syst-mandt ')' INTO w_system.
CONCATENATE 'List of all user IDs in' w_system
INTO w_line SEPARATED BY space.
* Export title to Word document.
CALL METHOD OF w_selection 'TypeText'
EXPORTING #1 = w_line.
CALL METHOD OF w_selection 'TypeParagraph'.
CALL METHOD OF w_selection 'TypeParagraph'.
SET PROPERTY OF w_paragraphformat 'Alignment' = 0.
SET PROPERTY OF w_font 'Size' = 10.
LOOP AT itab.
SET PROPERTY OF w_font 'Bold' = 1.
SET PROPERTY OF w_font 'Italic' = 0.
CALL METHOD OF w_selection 'TypeText'
EXPORTING #1 = itab-bname.
CONCATENATE space itab-name_text INTO w_line SEPARATED BY space.
SET PROPERTY OF w_font 'Bold' = 0.
SET PROPERTY OF w_font 'Italic' = 1.
* Export User Name and Full Name to Word document.
CALL METHOD OF w_selection 'TypeText'
EXPORTING #1 = w_line.
CALL METHOD OF w_selection 'TypeParagraph'.
ENDLOOP.
SET PROPERTY OF w_word 'Visible' = 1.