2007 Mar 07 2:56 PM
Hi all,
I have to make a report that get data from db tables and export it to a word doc.
The word should have data, including some lists, in boxes. No smartform, no ALV, just selection-screen>launch>open a word with data.
I have no idea how I do this. Can you help me, please? Thanks in advance. Reward points to good reply.
2007 Mar 07 3:26 PM
Hi,
Here is a small example to open a word from ABAP. this may help you to start.
Hi,
INCLUDE ole2incl.
DATA: word TYPE ole2_object, documentos TYPE
ole2_object,
documento TYPE ole2_object,
selection TYPE ole2_object,
font TYPE ole2_object.
CREATE OBJECT word 'WORD.APPLICATION'.
CALL METHOD OF word 'Documents' = documentos.
CALL METHOD OF documentos 'Add' = documento.
CALL METHOD OF documento 'Activate'.
GET PROPERTY OF word 'Selection' = selection.
GET PROPERTY OF selection 'Font' = font.
SET PROPERTY OF word 'Visible' = 1.
SET PROPERTY OF font 'Name' = 'Arial'.
SET PROPERTY OF font 'Size' = 12.
SET PROPERTY OF font 'Bold' = 1. "o 0
SET PROPERTY OF font 'Underline' = 1. "o 0
CALL METHOD OF selection 'TypeText' EXPORTING #1 = 'I am aRs'.
CALL METHOD OF selection 'TypeParagraph'.
CALL METHOD OF documento 'SaveAs' EXPORTING #1 = 'c:test.doc'.
CALL METHOD OF word 'Quit'.
aRs
Hi all,
I have to make a report that get data from db tables and export it to a word doc.
The word should have data, including some lists, in boxes. No smartform, no ALV, just selection-screen>launch>open a word with data.
I have no idea how I do this. Can you help me, please? Thanks in advance. Reward points to good reply.
2007 Mar 07 2:59 PM
Hi,
Check this link it may be useful.
http://help.sap.com/saphelp_nw2004s/helpdata/en/d1/af8841349e1909e10000000a155106/frameset.htm
Regards,
Sruthi
2007 Mar 07 3:02 PM
2007 Mar 07 3:26 PM
Hi,
Here is a small example to open a word from ABAP. this may help you to start.
Hi,
INCLUDE ole2incl.
DATA: word TYPE ole2_object, documentos TYPE
ole2_object,
documento TYPE ole2_object,
selection TYPE ole2_object,
font TYPE ole2_object.
CREATE OBJECT word 'WORD.APPLICATION'.
CALL METHOD OF word 'Documents' = documentos.
CALL METHOD OF documentos 'Add' = documento.
CALL METHOD OF documento 'Activate'.
GET PROPERTY OF word 'Selection' = selection.
GET PROPERTY OF selection 'Font' = font.
SET PROPERTY OF word 'Visible' = 1.
SET PROPERTY OF font 'Name' = 'Arial'.
SET PROPERTY OF font 'Size' = 12.
SET PROPERTY OF font 'Bold' = 1. "o 0
SET PROPERTY OF font 'Underline' = 1. "o 0
CALL METHOD OF selection 'TypeText' EXPORTING #1 = 'I am aRs'.
CALL METHOD OF selection 'TypeParagraph'.
CALL METHOD OF documento 'SaveAs' EXPORTING #1 = 'c:test.doc'.
CALL METHOD OF word 'Quit'.
aRs
2007 Mar 07 3:43 PM
Hi Andre,
Suggest you have a look at this thread - similar question
Thanks,
Janani
2007 Mar 07 4:09 PM
2007 Mar 07 4:38 PM
Another Example of Word.......
Report WordDoc.
*--Include for OLE-enabling definitions
INCLUDE ole2incl .
*--Global variables
*--Variables to hold OLE object and entity handles
DATA gs_word TYPE ole2_object . "OLE object handle
DATA gs_documents TYPE ole2_object . "Documents
DATA gs_actdoc TYPE ole2_object . "Active document
DATA gs_application TYPE ole2_object . "Application
DATA gs_options TYPE ole2_object . "Application options
DATA gs_actwin TYPE ole2_object . "Active window
DATA gs_actpan TYPE ole2_object . "Active pane
DATA gs_view TYPE ole2_object . "View
DATA gs_selection TYPE ole2_object . "Selection
DATA gs_font TYPE ole2_object . "Font
DATA gs_parformat TYPE ole2_object . "Paragraph format
DATA gs_tables TYPE ole2_object . "Tables
DATA gs_range TYPE ole2_object . "Range handle for various ranges
DATA gs_table TYPE ole2_object . "One table
DATA gs_table_border TYPE ole2_object . "Table border
DATA gs_cell TYPE ole2_object . "One cell of a table
DATA gs_paragraph TYPE ole2_object . "Paragraph
DATA gv_pos(5) TYPE n . "Position information for table
*Step 2 Creating the OLE object and get main entities to handle
*variables.
START-OF-SELECTION .
*--Creating OLE object handle variable
CREATE OBJECT gs_word 'WORD.APPLICATION' .
IF sy-subrc NE 0 .
MESSAGE s000(su) WITH 'Error while creating OLE object!'.
LEAVE PROGRAM .
ENDIF .
*--Setting object's visibility property
SET PROPERTY OF gs_word 'Visible' = '1' .
*--Opening a new document
GET PROPERTY OF gs_word 'Documents' = gs_documents .
CALL METHOD OF gs_documents 'Add' .
*--Getting active document handle
GET PROPERTY OF gs_word 'ActiveDocument' = gs_actdoc .
*--Getting applications handle
GET PROPERTY OF gs_actdoc 'Application' = gs_application .
*Code Part B.2 Creating the OLE object
*Step 3 Setting the measurement unit to #CM.#
*Code Part B.3 Setting measurement unit
*--Setting the measurement unit
GET PROPERTY OF gs_application 'Options' = gs_options .
SET PROPERTY OF gs_options 'MeasurementUnit' = '1' . "CM
*Step 4 Some header text.
*Code Part B.4 Setting header content
*--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 .
*--Setting font attributes
SET PROPERTY OF gs_font 'Name' = 'Arial' .
SET PROPERTY OF gs_font 'Size' = '10' .
SET PROPERTY OF gs_font 'Bold' = '0' . "Not bold
SET PROPERTY OF gs_font 'Italic' = '1' . "Italic
SET PROPERTY OF gs_font 'Underline' = '0' . "Not underlined
*--Setting paragraph format attribute
SET PROPERTY OF gs_parformat 'Alignment' = '2' . "Right-justified
CALL METHOD OF gs_selection 'TypeText'
EXPORTING
#1 = 'This is an OLE example!'.
*--Setting the view to the main document again
SET PROPERTY OF gs_view 'SeekView' = '0' . "Main document view
*Step 5 Writing the title.
*Code Part B.5 Writing the title
*--Reseting font attributes for the title
SET PROPERTY OF gs_font 'Name' = 'Times New Roman' .
SET PROPERTY OF gs_font 'Size' = '16' .
SET PROPERTY OF gs_font 'Bold' = '1' . "Bold
SET PROPERTY OF gs_font 'Italic' = '0' . "Not Italic
SET PROPERTY OF gs_font 'Underline' = '0' . "Not underlined
*--Setting paragraph format attribute
SET PROPERTY OF gs_parformat 'Alignment' = '1' . "Centered
CALL METHOD OF gs_selection 'TypeText'
EXPORTING
#1 = text-000.
*--Advancing cursor to the new line
CALL METHOD OF gs_selection 'TypeParagraph' .
*Step 6 Writing some text.
*Code Part B.6 Writing some text
*--Reseting font attributes for ordinary text
SET PROPERTY OF gs_font 'Name' = 'Times New Roman' .
SET PROPERTY OF gs_font 'Size' = '12' .
SET PROPERTY OF gs_font 'Bold' = '0' . "Not bold
SET PROPERTY OF gs_font 'Italic' = '0' . "Not Italic
SET PROPERTY OF gs_font 'Underline' = '0' . "Not underlined
*--Setting paragraph format attribute
SET PROPERTY OF gs_parformat 'Alignment' = '3' . "Justified
CALL METHOD OF gs_selection 'TypeText'
EXPORTING
#1 = text-001.
*--Skip some lines
DO 4 TIMES .
CALL METHOD OF gs_selection 'TypeParagraph' .
ENDDO .
*Step 7 Inserting a table and filling some of its cells.
*--Getting entity handles for the entities on the way
GET PROPERTY OF gs_actdoc 'Tables' = gs_tables .
GET PROPERTY OF gs_selection 'Range' = gs_range .
*--Adding a table with 3 rows and 2 columns
CALL METHOD OF gs_tables 'Add' = gs_table
EXPORTING
#1 = gs_range " Handle for range entity
#2 = '3' "Number of rows
#3 = '2'. "Number of columns
*--Setting border attribute for the table
GET PROPERTY OF gs_table 'Borders' = gs_table_border .
SET PROPERTY OF gs_table_border 'Enable' = '1' . "With border
*--Filling the table with dummy data
*--Reseting font attributes for table content
SET PROPERTY OF gs_font 'Name' = 'Garamond' .
SET PROPERTY OF gs_font 'Size' = '11' .
SET PROPERTY OF gs_font 'Bold' = '0' . "Not bold
SET PROPERTY OF gs_font 'Italic' = '0' . "Not Italic
SET PROPERTY OF gs_font 'Underline' = '0' . "Not underlined
*--Getting cell coordinates
CALL METHOD OF gs_table 'Cell' = gs_cell
EXPORTING
#1 = '1' "first row
#2 = '1'. "first column
*--Getting the range handle to write the text
GET PROPERTY OF gs_cell 'Range' = gs_range .
*--Filling the cell
SET PROPERTY OF gs_range 'Text' = 'OLE' .
*--Getting cell coordinates
CALL METHOD OF gs_table 'Cell' = gs_cell
EXPORTING
#1 = '3' "third row
#2 = '2'. "second column
*--Getting the range handle to write the text
*Code Part B.7 Some table work
GET PROPERTY OF gs_cell 'Range' = gs_range .
*--Filling the cell
SET PROPERTY OF gs_range 'Text' = 'OLE' .
*--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' .
*--Skip some lines
DO 3 TIMES .
CALL METHOD OF gs_selection 'TypeParagraph' .
ENDDO .
*Step 8 Adding some other text and indent its paragraph.
*Code Part B.8 Writing some indented text
*--Reseting font attributes for ordinary text
SET PROPERTY OF gs_font 'Name' = 'Times New Roman' .
SET PROPERTY OF gs_font 'Size' = '12' .
SET PROPERTY OF gs_font 'Bold' = '0' . "Not bold
SET PROPERTY OF gs_font 'Italic' = '0' . "Not Italic
SET PROPERTY OF gs_font 'Underline' = '0' . "Not underlined
*--Setting paragraph format attribute
SET PROPERTY OF gs_parformat 'Alignment' = '3' . "Justified
*--Indent the paragraph once
GET PROPERTY OF gs_selection 'Paragraphs' = gs_paragraph .
CALL METHOD OF gs_paragraph 'Indent' .
CALL METHOD OF gs_selection 'TypeText'
EXPORTING
#1 = text-002.
*Step 9 Freeing object handle variable to deallocate memory.
*Code Part B.9 Freeing object handle variable
FREE OBJECT gs_word.
2015 Jun 16 12:08 PM
hi ,
i use your code and its perfect , it work great in abap report , now i want use this in bsp , i export a table from bsp to program and the table is ok , but it did not show in word , i check the value of gs_word and gs_document and ... they were not correct , the component handle did not fill correct .
would you please explain why is not working ?
thank ,
reza .