..3 Viewer's user-enhanceability

Via IMPORTING-parameters IV_VIEWER_CALLBACK_PROG and IV_VIEWER_CALLBACK_FORM, You able to specify a CALLBACK-subroutine, that should be called at some events of the Viewer:

  • BEFORE_OLE_INITIALIZATION – is being raised in the Dynpro-PBO, before creating OLE-objects and showing the form via MS-Excel application. Here you can use third-party tools to bring some changes to the document, which available as changing-parameter cv_rawdata (HEX-sequence) .

  • AFTER_OLE_INITIALIZATION – is being raised in the Dynpro-PBO, before creating OLE-objects and showing the form via MS-Excel application. Here you can use OLE or DOI-technology for some manipulations with the form. For retrieving the actual form DOI-object, you should use subroutine VIEWER_GET_DOI_OBJECT.

  • CONTROLS_INIT – is being raised in the Dynpro-PBO, just once – at controls initialization (containers and toolbar). At the moment You able to add customer buttons to the toolbar.

  • FUNCTION_CODE – is being raised on any user-defined button click.

  • REDRAW – being raised at jump to a next workbook in the bundle.

Example of CALLBACK-subroutine using:

CALL FUNCTION 'ZXLWB_CALLFORM'

EXPORTING

iv_formname = 'SHIPPING_LABELS'

iv_context_ref = gt_context[]

iv_viewer_callback_prog = sy-cprog

iv_viewer_callback_form = 'CALLBACK_FORM'

EXCEPTIONS

OTHERS = 2.

IF sy-subrc NE 0 .

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 .

ENDIF .

*&---------------------------------------------------------------------*

*& Form CALLBACK_FORM

*&---------------------------------------------------------------------*

FORM callback_form

USING pv_event TYPE char50

CHANGING cv_fcode TYPE ui_func

cr_toolbar TYPE REF TO cl_gui_toolbar

cv_rawdata TYPE xstring . " -->> excel document as hex-string


CASE pv_event .

*=======================================================================

WHEN 'BEFORE_OLE_INITIALIZATION' . " here you can bring some changes to the CV_RAWDATA

* (excel document as HEX-string)

*=======================================================================


*=======================================================================

WHEN 'AFTER_OLE_INITIALIZATION' . " here you can use OLE or DOI

* to post-processing the excel document

*=======================================================================


* get DOI object

DATA:

lo_container_control TYPE REF TO i_oi_container_control ,

lo_document_proxy TYPE REF TO i_oi_document_proxy ,

lo_spreadsheet TYPE REF TO i_oi_spreadsheet .


PERFORM viewer_get_doi_object

IN PROGRAM saplzxlwb

CHANGING lo_container_control

lo_document_proxy

lo_spreadsheet .


CHECK lo_spreadsheet IS BOUND .


* CASE 1: print out an actual worksheet without print-dialog

lo_spreadsheet->print( name = 'Sheet1' ) .


* CASE 2: show print-dialog via DOI

lo_document_proxy->print_document(

EXPORTING prompt_user = abap_on

no_flush = abap_off ) .


* CASE 3: show print-dialog via OLE

DATA:

ls_handle TYPE cntl_handle ,

ls_application TYPE ole2_object ,

ls_activeworkbook TYPE ole2_object ,

ls_dialogs TYPE ole2_object ,

ls_item TYPE ole2_object .


lo_document_proxy->get_document_handle( IMPORTING handle = ls_handle ) .


CALL METHOD OF:

ls_handle-obj 'Application' = ls_application,

ls_application 'Dialogs' = ls_dialogs,

ls_dialogs 'Item' = ls_item EXPORTING #1 = '8',

ls_item 'Show'.


* CASE 4: save with protecting worksheet

GET PROPERTY OF ls_application 'ActiveWorkbook' = ls_activeworkbook .


CALL METHOD OF

ls_activeworkbook 'SaveAs'

EXPORTING

#1 = 'C:\protected.xlsx'

#2 = 51 " -->> FileFormat:=xlOpenXMLWorkbook

#3 = 'password'.


FREE OBJECT ls_item . CLEAR ls_item .

FREE OBJECT ls_dialogs . CLEAR ls_dialogs .

FREE OBJECT ls_activeworkbook . CLEAR ls_activeworkbook .

FREE OBJECT ls_application . CLEAR ls_application .


*=======================================================================

WHEN 'CONTROLS_INIT' . " here you can add custom buttons to the toolbar

*=======================================================================

TYPE-POOLS: cntb , icon .

cr_toolbar->add_button( fcode = 'NEW_BUTTON'

text = 'Show my message'

icon = icon_message_warning_small

butn_type = cntb_btype_button ) .


*=======================================================================

WHEN 'FUNCTION_CODE' . " here you can process function codes

*=======================================================================

CASE cv_fcode .

WHEN 'NEW_BUTTON' .

MESSAGE 'My message' TYPE 'I' .

WHEN OTHERS .

ENDCASE .


*=======================================================================

WHEN 'REDRAW' . " here you can process a jump

* to a next workbook in the bundle.

*=======================================================================


ENDCASE .

ENDFORM . "CALLBACK_FORM

Result (a customer-defined button in the application toolbar):