Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Create Word Document using OLE and contain table

Former Member
0 Likes
3,143

Hi,

I have been looking at various blogs etc to get example programs to create a word document using OLE. Some of these programs have code in them to create a table on the document.

For some reason the table is not being shown on the document and I cannot figure out why.

Has anybody got this to work so that a table will appear on a word document .

This is the logic I have: -


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

Thanks

Martin

1 ACCEPTED SOLUTION
Read only

former_member182371
Active Contributor
0 Likes
1,491

Hi,

have a look at this report:

SAPRDEMOWORD97INTEGRATION

Best regards.

Hi,

I have been looking at various blogs etc to get example programs to create a word document using OLE. Some of these programs have code in them to create a table on the document.

For some reason the table is not being shown on the document and I cannot figure out why.

Has anybody got this to work so that a table will appear on a word document .

This is the logic I have: -


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

Thanks

Martin

3 REPLIES 3
Read only

former_member182371
Active Contributor
0 Likes
1,492

Hi,

have a look at this report:

SAPRDEMOWORD97INTEGRATION

Best regards.

Read only

0 Likes
1,491

Hi Pablo,

It appears this prorgam is not using OLE. I am using OLE.

Thanks

Martin

Read only

0 Likes
1,491

Hi,

maybe this wiki can help:

http://wiki.sdn.sap.com/wiki/display/ABAP/DownloadDataintoWordDocumentusingOLE+Automation

i´ve just slightly adapted the above wiki and it works:


REPORT ztest1.

INCLUDE ole2incl.

*----------------------------------------------*
*   TABLES                                     *
*----------------------------------------------*

TABLES: ekpo.

*----------------------------------------------*
*   DATA DECLARATION                           *
*----------------------------------------------*

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 . "Application

DATA : wf_lines TYPE i,
       wf_counter TYPE i.

*----------------------------------------------*
*   Internal Tables                            *
*----------------------------------------------*

DATA : BEGIN OF tb_ekpo OCCURS 0,
        ebeln TYPE ekpo-ebeln,
        ebelp TYPE ekpo-ebelp,
       END OF tb_ekpo.


*----------------------------------------------------------------------*
*  Start of Selection                                                              *
*----------------------------------------------------------------------*

START-OF-SELECTION.

  SELECT
      ebeln
      ebelp
       FROM
       ekpo
       INTO TABLE tb_ekpo UP TO 10 ROWS.

END-OF-SELECTION.


  CREATE OBJECT gs_word 'WORD.APPLICATION'. "Create word object
*  Setting object's visibility property
  SET PROPERTY OF gs_word 'Visible' = 0.
*  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.

  SET PROPERTY OF gs_font 'Name' = 'Arial'.
  SET PROPERTY OF gs_font 'Size' = '16'.
  SET PROPERTY OF gs_font 'Bold' = '1'.
  SET PROPERTY OF gs_font 'Italic' = '1'.
  SET PROPERTY OF gs_font 'Underline' = '1'.
  SET PROPERTY OF gs_parformat 'Alignment' = '1'. " Centered

  CALL METHOD OF gs_selection 'TypeText'
    EXPORTING
    #1 = 'PO Details'.

  DESCRIBE TABLE tb_ekpo 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 = '2'. "Columns
  GET PROPERTY OF gs_table 'Borders' = gs_border.
  SET PROPERTY OF gs_border 'Enable' = '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' = 'EBELN'.

  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' = 'EBELP'.



  LOOP AT tb_ekpo.
    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' = tb_ekpo-ebeln.

    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' = tb_ekpo-ebelp.

  ENDLOOP.

  CALL METHOD OF gs_document 'SaveAs'
    EXPORTING
    #1 = 'C:UsersxxxxxxxxDownloadsole.doc'.

  CALL METHOD OF gs_word 'Quit'.

  FREE OBJECT gs_word.

Best regards.

Edited by: Pablo Casamayor on Dec 9, 2011 5:36 PM

Edited by: Pablo Casamayor on Dec 9, 2011 5:38 PM