SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Desktop Office Integration DOI



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> OLE2, Excel, WinWord
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1636

PostPosted: Sun Mar 09, 2008 11:03 am    Post subject: Desktop Office Integration DOI Reply with quote

Original: http: //www.erpgenie.com/abap/code/abap21.htm

Code:
PROGRAM z_office_demo MESSAGE-ID demoofficeintegratio.
* this program is able to run desktop applications like Excel, Word,
* PowerPoint, WordPad, Word Pro, Lotus 1-2-3, and Visio either in-place
* (in R/3 screen) or in their own windows. To determine the desktop
* application you have to enter the associated "ProgID" in the entry
* field "Desktop application" (check the Registry Editor to confirm ProgIDs)
* for Word 97: WORD.DOCUMENT.8
* for Excel: EXCEL.SHEET.8
* for PowerPoint: POWERPOINT.SHOW.8
* for Word Pro: WORDPRO.DOCUMENT
* for Lotus 1-2-3: LOTUS123.WORKBOOK
* for Visio: VISIO.DRAWING.5
* for WordPad: WORDPAD.DOCUMENT

* start with the only screen 100
SET SCREEN 100.

* Control Framework definitions. These definitions have to be included
* for use of controls and SAP DOI makes use of control technology
TYPE-POOLS: cndp.
INCLUDE <ctldef>.

* this is the very important INCLUDE. It contains the whole ABAP
* class and interface definitions and implementations of
* SAP Desktop Office Integration. These definitions are global from
* 4.6A on, so you won't need this INCLUDE there.
INCLUDE officeintegrationinclude.

DATA: control TYPE REF TO i_oi_ole_container_control,
      factory TYPE REF TO i_oi_document_factory,
      document TYPE REF TO i_oi_document_proxy.
* the dynpro entry fields
DATA: application(25) TYPE c VALUE 'Word.Document.8',
      inplace(1) TYPE c.

DATA: stored_application LIKE application,
      initialized(1), retcode TYPE t_oi_ret_string,
      is_released TYPE i, has_changed TYPE i.

TYPES:row(1024) TYPE c.
* ABAP internal table to store documents of desktop applications
DATA: doc_table TYPE STANDARD TABLE OF row,
      doc_size TYPE i, doc_url(256).

* in ABAP you have to write an event handler class to catch events.
* This is ABAP OO technology. SAP DOI just makes use of it to
* catch the event: "user closes desktop application by closing window"
CLASS c_event_handler DEFINITION.
  PUBLIC SECTION.
* catch the event: "user closes desktop application by closing window"
    CLASS-METHODS: close_event_handler
    FOR EVENT on_close_document OF i_oi_document_proxy
    IMPORTING document_proxy has_changed.

* for trigger this event you have to write a VB statement
* like ActiveDocument.Container.SendCustomEvent("param1", ...)
* but for now we don't support this (empty implementation)
    CLASS-METHODS: custom_event_handler
    FOR EVENT on_custom_event OF i_oi_document_proxy
    IMPORTING document_proxy event_name param_count
    param1 param2 param3.
ENDCLASS.                    "c_event_handler DEFINITION

*----------------------------------------------------------------------*
*       CLASS c_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS c_event_handler IMPLEMENTATION.
  METHOD close_event_handler.
    DATA: answer.
* ask user if document is to be stored
    CALL FUNCTION 'POPUP_TO_CONFIRM'
      EXPORTING
        titlebar              = 'Office Integration Demo'
        text_question         = 'Save Document?'
        display_cancel_button = ' '
      IMPORTING
        answer                = answer
      EXCEPTIONS
        text_not_found        = 1
        OTHERS                = 2.
    IF answer = '1'.
* user said store it !
      PERFORM: store_document.
    ENDIF.
    PERFORM close_document.
  ENDMETHOD.                    "close_event_handler

  METHOD custom_event_handler.
* for now we don't support this method (empty implementation)
  ENDMETHOD.                    "custom_event_handler
ENDCLASS.                    "c_event_handler IMPLEMENTATION

*----------------------------------*
* MODULE BEFORE_0100 OUTPUT *
*----------------------------------*
* initializes Control Framework just once *
*----------------------------------*
MODULE before_0100 OUTPUT.
  SET PF-STATUS 'MAIN0100'.
  SET TITLEBAR '001'.
  IF initialized IS INITIAL.
    CALL FUNCTION 'CONTROL_INIT'
      EXCEPTIONS
        control_init_error = 1
        OTHERS             = 2.
    IF sy-subrc NE 0. MESSAGE e007. ENDIF.
    initialized = 'X'.
  ENDIF.
ENDMODULE.                    "before_0100 OUTPUT

*----------------------------------*
* MODULE USER_COMMAND_0100 INPUT *
*----------------------------------*
* handles all R/3 commands like create, store, reopen *
* as well as the standard exit, return and abort icons *
*----------------------------------*
MODULE user_command_0100 INPUT.
* CONTROL_DISPATCH is in 4.0 and 4.5 necessary to
* dispatch control events to the controls.
* Control events are okcodes, which start with '%'
  IF sy-ucomm CA '%'.
    CALL FUNCTION 'CONTROL_DISPATCH'
      EXPORTING
        fcode = sy-ucomm.
* exceptions
* cb_not_found = 1
* others = 2.
    EXIT.
  ENDIF.

  CASE sy-ucomm.
    WHEN 'CREATE'.
* start an application and create a new document
* first close current application, if one exists
      PERFORM close_document.
* application in its window - factory points to i_oi_document_factory
      IF factory IS INITIAL.
        PERFORM create_factory.
      ENDIF.
* in-place activation - control pointing to i_oi_ole_container_control
      IF control IS INITIAL.
        PERFORM create_container.
      ENDIF.
* document pointing to i_oi_document:proxy interface
      PERFORM get_document_proxy USING application.
* register the event handler method for closing the document window
      SET HANDLER c_event_handler=>close_event_handler FOR document.

      CALL METHOD document->create_document
        EXPORTING
          open_inplace = inplace.
      CALL METHOD c_oi_errors=>show_message
        EXPORTING
          type = 'E'.

    WHEN 'STORE'.
* close document and save it in ABAP table doc_table
      PERFORM store_document.
      stored_application = application.
      PERFORM close_document.

* reopen the document stored in doc_table
    WHEN 'REOPEN'.
      CHECK doc_size > 0.
      PERFORM close_document.
      PERFORM get_document_proxy USING stored_application.
* register the event handler method for closing the document window
      SET HANDLER c_event_handler=>close_event_handler FOR document.
      PERFORM open_document_from_table.

    WHEN 'EXIT'.
      PERFORM close_document.
      IF NOT control IS INITIAL.
        CALL METHOD control->destroy_control.
        FREE control.
      ENDIF.
      IF NOT factory IS INITIAL.
        CALL METHOD factory->stop_factory.
        FREE factory.
      ENDIF.
      CALL FUNCTION 'CONTROL_EXIT'.
      LEAVE PROGRAM.
  ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT

*----------------------------------*
* FORM CREATE_CONTAINER *
*----------------------------------*
FORM create_container.
* create an i_oi_ole_container_control interface instance
* in case of in-place activation, referenced by CONTROL
  CALL METHOD c_oi_ole_control_creator=>get_ole_container_control
    IMPORTING
      control = control
      retcode = retcode.
  CALL METHOD c_oi_errors=>show_message
    EXPORTING
      type = 'E'.

  DATA: shell_style TYPE i, align TYPE i, inplace_mode TYPE i.
  shell_style = ws_visible + ws_child + ws_border + ws_clipchildren.
  inplace_mode = control->inplace_mode_enabled +
  control->inplace_mode_scroll.

  CALL METHOD control->init_control
    EXPORTING
      r3_application_name     = 'R/3 Basis'
      inplace_mode            = inplace_mode
      shell_style             = shell_style
      register_on_close_event = 'X'
    RECEIVING
      retcode                 = retcode.
  CALL METHOD c_oi_errors=>show_message
    EXPORTING
      type = 'E'.

  align = align_at_right + align_at_bottom.
  CALL METHOD control->set_window_properties
    EXPORTING
      top   = 5
      align = align.
  CALL METHOD c_oi_errors=>show_message
    EXPORTING
      type = 'E'.
ENDFORM.                    "create_container

*----------------------------------*
* FORM CREATE_FACTORY *
*----------------------------------*
FORM create_factory.
* create an i_oi_document_factory interface instance
* in case of own application window activation referenced
* FACTORY
  CALL METHOD c_oi_factory_creator=>get_document_factory
    EXPORTING
      factory_type = 'OLE'
    IMPORTING
      factory      = factory
      retcode      = retcode.
  CALL METHOD c_oi_errors=>show_message
    EXPORTING
      type = 'E'.
  CALL METHOD factory->start_factory
    EXPORTING
      r3_application_name     = 'SAP DOI'
      register_on_close_event = 'X'
    RECEIVING
      retcode                 = retcode.
ENDFORM.                    "create_factory

*----------------------------------*
* FORM STORE_DOCUMENT *
*----------------------------------*
* store document in internal table doc_table
* doc_size has length of table in charackters
*----------------------------------*
FORM store_document.
  CHECK NOT document IS INITIAL.
  CALL METHOD document->is_destroyed
    RECEIVING
      ret_value = is_released.
  IF is_released IS INITIAL.
    CALL METHOD document->close_document
      EXPORTING
        do_save     = 'X'
      IMPORTING
        has_changed = has_changed.
    CALL METHOD c_oi_errors=>show_message
      EXPORTING
        type = 'E'.
    IF NOT has_changed IS INITIAL.
* document has changed, so save it into ABAP internal table
      CALL FUNCTION 'SAP_OI_GET_UNIQUE_URL'
        IMPORTING
          unique_url = doc_url
        EXCEPTIONS
          OTHERS     = 0.
      CALL METHOD document->save_document_to_url
        EXPORTING
          url     = doc_url
        RECEIVING
          retcode = retcode.
      CALL METHOD c_oi_errors=>show_message
        EXPORTING
          type = 'E'.
      CALL FUNCTION 'DP_GET_STREAM_FROM_URL'
        EXPORTING
          url  = doc_url
        IMPORTING
          size = doc_size
        TABLES
          data = doc_table.
* exceptions
* dp_fail = 1
* dp_failed_init = 2
* others = 3.
      CALL METHOD c_oi_errors=>show_message
        EXPORTING
          type = 'E'.
    ENDIF.
  ENDIF.
ENDFORM.                    "store_document

*----------------------------------*
* FORM CLOSE_DOCUMENT *
*----------------------------------*
FORM close_document.
  CHECK NOT document IS INITIAL.
  CALL METHOD document->close_document.
  CALL METHOD document->release_document.
  FREE document.
ENDFORM.                    "close_document

*----------------------------------*
* FORM GET_DOCUMENT_PROXY *
*----------------------------------*
* will create a reference to the interface i_oi_document_proxy *
* and assign it to DOCUMENT. The document type is determined *
* by the APPLICATION parameter. *
*----------------------------------*
FORM get_document_proxy USING application.
  IF inplace IS INITIAL.
    CALL METHOD factory->get_document_proxy
      EXPORTING
        document_type  = application
      IMPORTING
        document_proxy = document
        retcode        = retcode.
  ELSE.
    CALL METHOD control->get_document_proxy
      EXPORTING
        document_type  = application
      IMPORTING
        document_proxy = document
        retcode        = retcode.
  ENDIF.
  CALL METHOD c_oi_errors=>show_message
    EXPORTING
      type = 'E'.
ENDFORM.                    "get_document_proxy

*----------------------------------*
* FORM OPEN_DOCUMENT_FROM_TABLE *
*----------------------------------*
* will open a document, which is stored in the ABAP internal *
* table DOC_TABLE usinf the data provider (DP_CREATE_URL) *
* DOCUMENT points to an i_oi_document_proxy interface. *
*----------------------------------*
FORM open_document_from_table.
* transport doc_table contents to desktop and get back a key in doc_url
* to reference the table contents
  CALL FUNCTION 'DP_CREATE_URL'
    EXPORTING
      type                 = 'application'
      subtype              = 'x-oleobject'
      size                 = doc_size
    TABLES
      data                 = doc_table
    CHANGING
      url                  = doc_url
    EXCEPTIONS
      dp_invalid_parameter = 1
      dp_error_put_table   = 2
      dp_error_general     = 3
      OTHERS               = 4.
  CASE sy-subrc.
    WHEN 0.
    WHEN 1. MESSAGE e802 RAISING dp_invalid_parameter.
    WHEN 2. MESSAGE e802 RAISING dp_error_put_table.
    WHEN 3. MESSAGE e802 RAISING dp_error_general.
    WHEN 4. MESSAGE e802 RAISING dp_error_general.
  ENDCASE.
  IF NOT doc_url IS INITIAL.
* now open the document referenced at the frontend by doc_url
    CALL METHOD document->open_document
      EXPORTING
        document_url = doc_url
        open_inplace = inplace
      RECEIVING
        retcode      = retcode.
    CALL METHOD c_oi_errors=>show_message
      EXPORTING
        type = 'E'.
  ELSE.
    MESSAGE e010.
  ENDIF.
ENDFORM.                    "open_document_from_table
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> OLE2, Excel, WinWord All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.