Skip to Content
avatar image
Former Member

Word document using OLE automation in multiple pages

Hello all,

We have a requirement to create one word document for 34 tables.These 34 tables should display on 34 pages (Each table in one page).

So we have created Z program with OLE programming. Currently we are trying to display two tables on 2 pages.But both tables are getting displayed on page 2. Page 1 is Blank.

Please find the code attached herewith.

INCLUDE ole2incl.
DATA :

BEGIN OF IT_CC OCCURS 0,
MANDT TYPE ZCC-MANDT,
BUKRS TYPE ZCC-BUKRS,
BUTXT TYPE ZCC-BUTXT,
FROMDATE TYPE ZCC-FROMDATE,
TODATE TYPE ZCC-TODATE,

END OF IT_CC.


DATA : WF_LINES TYPE I,
WF_COUNTER TYPE I.

DATA: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_APPLICATION TYPE OLE2_OBJECT,
GS_WORD2 TYPE OLE2_OBJECT, "Word Object
GS_DOCUMENTS2 TYPE OLE2_OBJECT, "List of documents
GS_DOCUMENT2 TYPE OLE2_OBJECT, "Current / Active document
GS_SELECTION2 TYPE OLE2_OBJECT, "Current Cursor Selection
GS_ACTDOC2 TYPE OLE2_OBJECT , "Active document
GS_FONT2 TYPE OLE2_OBJECT , "Font
GS_PARFORMAT2 TYPE OLE2_OBJECT , "Paragraph format
GS_TABLES2 TYPE OLE2_OBJECT , "Tables
GS_TABLE2 TYPE OLE2_OBJECT,
GS_RANGE2 TYPE OLE2_OBJECT,
GS_CELL2 TYPE OLE2_OBJECT,
GS_BORDER2 TYPE OLE2_OBJECT,
GS_APPLICATION2 TYPE OLE2_OBJECT,
WA_CC LIKE LINE OF IT_CC.


START-OF-SELECTION.

SELECT MANDT BUKRS BUTXT FROMDATE TODATE FROM ZCC INTO TABLE IT_CC.

CREATE OBJECT gs_word 'WORD.APPLICATION'.

SET PROPERTY OF gs_word 'VISIBLE' = '1'.
CALL METHOD OF gs_word 'Documents' = gs_documents.

CALL METHOD OF gs_documents 'Add' = gs_document.

CALL METHOD OF gs_document 'Activate'.

GET PROPERTY OF GS_WORD 'ActiveDocument' = GS_ACTDOC2.

GET PROPERTY OF GS_ACTDOC2 'Application' = GS_APPLICATION2

.* Getting handle for the selection which is here the character at the cursor position

GET PROPERTY OF GS_APPLICATION2 'Selection' = GS_SELECTION2.

** Gets the cursor position

GET PROPERTY OF gs_word 'SELECTION' = gs_selection.

* 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.



** To display text in 1st page

CALL METHOD OF gs_selection 'TYPETEXT'

EXPORTING
#1 = 'This is the Page One text!'.
GET PROPERTY OF GS_ACTDOC 'Tables' = GS_TABLES2.

GET PROPERTY OF GS_SELECTION 'Range' = GS_RANGE2.

CALL METHOD OF GS_TABLES2 'Add' = GS_TABLE2

EXPORTING

#1 = GS_RANGE2
#2 = '3' " Rows
#3 = '1'. "Columns

GET PROPERTY OF GS_TABLE2 'Borders' = GS_BORDER2.

SET PROPERTY OF GS_BORDER2 'Enable' = '1'.

wf_counter = 1.

CALL METHOD OF GS_TABLE2 'Cell' = GS_CELL2

EXPORTING

#1 = '1'
#2 = '1'.

GET PROPERTY OF GS_CELL2 'Range' = GS_RANGE2.

SET PROPERTY OF GS_RANGE2 'Text' = 'Client'.

CALL METHOD OF GS_TABLE2 'Cell' = GS_CELL2

EXPORTING

#1 = '2'
#2 = '1'.

GET PROPERTY OF GS_CELL2 'Range' = GS_RANGE2.

SET PROPERTY OF GS_RANGE2 'Text' = 'Company Code'.

CALL METHOD OF GS_TABLE2 'Cell' = GS_CELL2

EXPORTING

#1 = '3'
#2 = '1'.

GET PROPERTY OF GS_CELL2 'Range' = GS_RANGE2.

SET PROPERTY OF GS_RANGE2 'Text' = 'Company Name'.

** Inserts a page break

CALL METHOD OF gs_selection 'INSERTBREAK'.


** To display text in 2nd page

CALL METHOD OF gs_selection 'TYPETEXT'

EXPORTING
#1 = 'This is the Page Two text!'.


DESCRIBE TABLE IT_CC LINES WF_LINES.
WF_LINES = WF_LINES + 1.

GET PROPERTY OF GS_ACTDOC 'Tables' = GS_TABLES.

GET PROPERTY OF GS_SELECTION 'Range' = GS_RANGE.

CALL METHOD OF GS_TABLES 'Add' = GS_TABLE

EXPORTING

#1 = GS_RANGE
#2 = WF_LINES " Rows
#3 = '5'. "Columns

GET PROPERTY OF GS_TABLE 'Borders' = GS_BORDER.

SET PROPERTY OF GS_BORDER 'Enable' = '1'.

wf_counter = 1.

CALL METHOD OF GS_TABLE 'Cell' = GS_CELL

EXPORTING

#1 = '1'
#2 = '1'.

GET PROPERTY OF GS_CELL 'Range' = GS_RANGE.

SET PROPERTY OF GS_RANGE 'Text' = 'Client'.

CALL METHOD OF GS_TABLE 'Cell' = GS_CELL

EXPORTING

#1 = '1'
#2 = '2'.

GET PROPERTY OF GS_CELL 'Range' = GS_RANGE.

SET PROPERTY OF GS_RANGE 'Text' = 'Company Code'.

CALL METHOD OF GS_TABLE 'Cell' = GS_CELL

EXPORTING

#1 = '1'
#2 = '3'.

GET PROPERTY OF GS_CELL 'Range' = GS_RANGE.

SET PROPERTY OF GS_RANGE 'Text' = 'Company Name'.

CALL METHOD OF GS_TABLE 'Cell' = GS_CELL

EXPORTING

#1 = '1'
#2 = '4'.

GET PROPERTY OF GS_CELL 'Range' = GS_RANGE.

SET PROPERTY OF GS_RANGE 'Text' = 'From Date'.

CALL METHOD OF GS_TABLE 'Cell' = GS_CELL

EXPORTING

#1 = '1'

#2 = '5'.

GET PROPERTY OF GS_CELL 'Range' = GS_RANGE.

SET PROPERTY OF GS_RANGE 'Text' = 'To Date'.

CALL METHOD OF GS_TABLE 'Cell' = GS_CELL

EXPORTING

#1 = '1'
#2 = '6'.
loop at it_cc into wa_cc.

SET PROPERTY OF GS_FONT 'Name' = 'Arial'.

SET PROPERTY OF GS_FONT 'Size' = '14'.

SET PROPERTY OF GS_FONT 'Bold' = '0'.

SET PROPERTY OF GS_FONT 'Italic' = '1'.

SET PROPERTY OF GS_FONT 'Underline' = '0'.

SET PROPERTY OF GS_PARFORMAT 'Alignment' = '1'.
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_cc-mandt.

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_cc-bukrs.

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_cc-butxt.

CALL METHOD OF GS_TABLE 'Cell' = GS_CELL

EXPORTING

#1 = wf_counter
#2 = '4'.

GET PROPERTY OF GS_CELL 'Range' = GS_RANGE.

SET PROPERTY OF GS_RANGE 'Text' = wa_cc-fromdate

.CALL METHOD OF GS_TABLE 'Cell' = GS_CELL

EXPORTING

#1 = wf_counter
#2 = '5'.

GET PROPERTY OF GS_CELL 'Range' = GS_RANGE.

SET PROPERTY OF GS_RANGE 'Text' = wa_cc-todate

CALL METHOD OF GS_TABLE 'Cell' = GS_CELL

EXPORTING

#1 = wf_counter
#2 = '6'.
ENDLOOP.

** Save Word document*Please change the path of the file appropriately

CALL METHOD OF gs_document 'SaveAs'EXPORTING
#1 = 'C:\Users\Desktop\Test.doc'.
CALL METHOD OF gs_word 'Quit'.
FREE OBJECT gs_word.

Please Help.

Regards,

Anuja Dhondge

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Dec 19, 2016 at 10:06 PM

    I would check the attributes in the created document - hidden characters, style, etc. Wait, correction: I would first question what business purpose could be possibly served by creating a 34 table/page Word document from SAP and how this would be maintainable in the long run (with S/4HANA, cloud and such).

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Dec 20, 2016 at 06:01 AM

    Hello Jelena,

    It is an assessment report for the financials of the company for which the data is maintained in 34 tables .

    Please help.

    Regards,

    Anuja Dhondge

    Add comment
    10|10000 characters needed characters exceeded