OLE from SAP

Here is an example of how to do OLE from SAP.

Here is an example of how to do OLE from SAP. The code is given for OLE for Excel and SAP version 4.6x.


REPORT ZOLE .

TABLES: MARA.
INCLUDE OLE2INCL.

DATA: BEGIN OF ITAB OCCURS 0,
MATNR LIKE MARA-MATNR,
MTART LIKE MARA-MTART,
ERSDA LIKE MARA-ERSDA,
DATAB LIKE MARA-DATAB,
END OF ITAB.


DATA: STR(20),NDATE(10).
DATA: ROW TYPE I VALUE 1, COL TYPE I VALUE 1.
DATA OBJ TYPE OLE2_OBJECT.
DATA WORKBOOK TYPE OLE2_OBJECT.
DATA SH TYPE OLE2_OBJECT.
DATA CELL TYPE OLE2_OBJECT.
DATA RANGE TYPE OLE2_OBJECT.
DATA APP TYPE OLE2_OBJECT.
DATA SEL TYPE OLE2_OBJECT.
DATA COLUMNS TYPE OLE2_OBJECT.

SELECT * INTO CORRESPONDING FIELDS OF TABLE ITAB FROM MARA.

CREATE OBJECT OBJ 'excel.application'.
*sET PROPERTY OF OBJ 'SheetsInNewWorkbook' = 1.
SET PROPERTY OF OBJ 'visible' = 1.
CALL METHOD OF OBJ 'Workbooks' = WORKBOOK.
WRITE :/ 'workbook', SY-SUBRC.
CALL METHOD OF WORKBOOK 'add'.
CALL METHOD OF OBJ 'Worksheets' = SH EXPORTING #1 = 1.
CALL METHOD OF SH 'Activate'.

PERFORM INSTERDATA USING ROW COL 'Material No.'.
COL = COL + 1.
PERFORM INSTERDATA USING ROW COL 'Material Type'.
COL = COL + 1.
PERFORM INSTERDATA USING ROW COL 'Creation Date'.
COL = COL + 1.
PERFORM INSTERDATA USING ROW COL 'Valid From'.
ROW = ROW + 1.

LOOP AT ITAB TO 100.
  COL = 1.
  PERFORM INSTERDATA USING ROW COL ITAB-MATNR.
  COL = COL + 1.
  PERFORM INSTERDATA USING ROW COL ITAB-MTART.
  COL = COL + 1.
  WRITE ITAB-ERSDA DD/MM/YYYY TO NDATE.
  PERFORM INSTERDATA USING ROW COL NDATE.
  COL = COL + 1.
  WRITE ITAB-DATAB DD/MM/YYYY TO NDATE.
  PERFORM INSTERDATA USING ROW COL NDATE.
  ROW = ROW + 1.
ENDLOOP.
STR = ROW.
CONCATENATE 'D1:D' STR INTO STR.
WRITE:/ STR.
CALL METHOD OF SH 'range' = RANGE EXPORTING #1 = 'A1:A32'.
PERFORM AUTOFIT.
CALL METHOD OF SH 'range' = RANGE EXPORTING #1 = 'B1:B32'.
PERFORM AUTOFIT.
CALL METHOD OF SH 'range' = RANGE EXPORTING #1 = 'C1:C32'.
PERFORM AUTOFIT.
CALL METHOD OF SH 'range' = RANGE EXPORTING #1 = 'D1:D32'.
PERFORM AUTOFIT.

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

*& Form INSTERDATA
*&---------------------------------------------------------------------*

* text
*----------------------------------------------------------------------*

* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*

FORM INSTERDATA USING ROW COL VALUE.
  CALL METHOD OF SH 'Cells' = CELL EXPORTING #1 = ROW #2 = COL.
  SET PROPERTY OF CELL 'value' = VALUE.
ENDFORM. " INSTERDATA
*&---------------------------------------------------------------------*

*& Form AUTOFIT
*&---------------------------------------------------------------------*

* text
*----------------------------------------------------------------------*

* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*

FORM AUTOFIT.
  CALL METHOD OF RANGE 'Select'.
  WRITE:/ 'autofit',SY-SUBRC.
  CALL METHOD OF SH 'application' = APP.
  CALL METHOD OF APP 'selection' = SEL.
  CALL METHOD OF SEL 'Columns' = COLUMNS.
  CALL METHOD OF COLUMNS 'autofit'.

ENDFORM. " AUTOFITREPORT ZOLE.

This was first published in November 2002

Dig deeper on SAP xApps

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close