Skip to Content
author's profile photo Former Member
Former Member

Create Word Document using OLE and contain table

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Best Answer
    Posted on Dec 09, 2011 at 03:09 PM

    Hi,

    have a look at this report:

    SAPRDEMOWORD97INTEGRATION

    Best regards.

    Add a comment
    10|10000 characters needed characters exceeded

    • 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

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.