that_girl
that_girl

Reputation: 11

How to display simple text on an embedded Word document within SAP GUI using OLE without opening an external Word instance?

I'm working on an SAP ABAP program that embeds a Microsoft Word document into the SAP GUI container using i_oi_document_proxy from SAP's Office Integration. I need to display a simple text, retrieved from an internal table, directly inside this embedded document without opening an external Word instance. My goal is to ensure the text appears within the integrated Word document in the SAP GUI container itself.

I wanted to work with OLE 2 at first as it offers simple manipulation for the document, but it opens a new instance of word independent and not in the container, I didn't know how to make ole2 work on the document created with i_oi_document_proxy. Is there a way to do so? If not, is there a way to display a simple text from the ABAP code to the integrated Word document!!!

I've tried several methods to load and display the text:

Populating the internal table with sample text and attempting to load it into the embedded document.

Using OLE2, but this approach often creates a separate Word instance outside the SAP GUI container, which is not what I want. My expectation is to use OLE (or an alternative approach, if available) to manipulate the embedded Word document directly, displaying text in it without launching Word independently. here is my code:

*&---------------------------------------------------------------------*
*& Report ZDOCUMENT_OLE_COMBINED
*&---------------------------------------------------------------------*
*& This program embeds a Word document in the SAP GUI using OLE,
*& and provides enhanced Word manipulation functionalities.
*&---------------------------------------------------------------------*

REPORT ZDOCUMENT_OLE_COMBINED.

*Include OLE definitions for enabling OLE manipulations
INCLUDE OLE2INCL.

DATA: container   TYPE REF TO cl_gui_custom_container,
      control     TYPE REF TO i_oi_container_control,
      document    TYPE REF TO i_oi_document_proxy,
      data_table  TYPE STANDARD TABLE OF soli,   " Internal table to hold the document data
      data_size   TYPE i,                        " Size of the document data
      retcode     TYPE i.                        " Return code for the save operation.

DATA: gs_word TYPE ole2_object,           " OLE object handle for Word
      gs_documents TYPE ole2_object,      " Document handle for Documents
      gs_actdoc TYPE ole2_object,         " Active document
      gs_selection TYPE ole2_object,      " Selection object for text formatting
      gs_font TYPE ole2_object.           " Font object for style adjustments

*---------------------------------------------------------------------*
* Start program by calling SCREEN 100 for UI display
*---------------------------------------------------------------------*
START-OF-SELECTION.
  CALL SCREEN 0100. " Direct SAP to show the UI screen with embedded Word document

*---------------------------------------------------------------------*
* Define screen status and handling logic
*---------------------------------------------------------------------*
MODULE status_100 OUTPUT.
  SET PF-STATUS 'MAIN0100'. " Sets the function key status for the screen
  SET TITLEBAR '001'. " Sets the title bar for the screen
ENDMODULE.

MODULE user_command_100 INPUT.
  CASE sy-ucomm.
    WHEN 'CREATE'.
      PERFORM create_new_document.
*    WHEN 'OPEN'.
*      PERFORM format_text_example.
    WHEN 'SAVE'.
      PERFORM save_document_to_table.
    WHEN 'EXIT'.
      LEAVE PROGRAM.
  ENDCASE.
ENDMODULE.
* *---------------------------------------------------------------------*
** FORM to create a new Word document
**---------------------------------------------------------------------*
FORM create_new_document.
  IF container IS INITIAL.
    CREATE OBJECT container
      EXPORTING
        container_name = 'CONTAINER'. " Container defined in the screen
  ENDIF.

* Initialize OLE container control for MS Word
  CALL METHOD C_OI_CONTAINER_CONTROL_CREATOR=>GET_CONTAINER_CONTROL
    IMPORTING
      control = control.

  CALL METHOD control->init_control
    EXPORTING
      r3_application_name    = 'Word Integration'
      inplace_enabled        = 'X'   " Enables embedding inside SAP
      inplace_scroll_documents = 'X'
      parent                 = container
      register_on_close_event = 'X'
      no_flush               = ' '.

* Initialize embedded Word document
  CALL METHOD control->get_document_proxy
    EXPORTING
      document_type = 'Word.Document.8'
    IMPORTING
      document_proxy = document.

  CALL METHOD document->create_document
    EXPORTING
      open_inplace = 'X'.



ENDFORM.
*---------------------------------------------------------------------*
* FORM to save document into an internal table
*---------------------------------------------------------------------*
FORM save_document_to_table.
  IF NOT document IS INITIAL.
    CALL METHOD document->save_document_to_table
      CHANGING
        document_table = data_table
        document_size  = data_size.

    IF retcode = 0.
      MESSAGE 'Document saved to internal table successfully.' TYPE 'I'.
    ELSE.
      MESSAGE 'Error saving document.' TYPE 'E'.
    ENDIF.
  ENDIF.
ENDFORM.

*---------------------------------------------------------------------*
* Exit logic to clean up objects and free resources
*---------------------------------------------------------------------*
MODULE exit_100 INPUT.
  IF NOT document IS INITIAL.
    CALL METHOD document->close_document.
    FREE document.
  ENDIF.

  IF NOT control IS INITIAL.
    CALL METHOD control->destroy_control.
    FREE control.
  ENDIF.

  LEAVE PROGRAM.
ENDMODULE.

Upvotes: 1

Views: 157

Answers (0)

Related Questions