Tip

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

    Requires Free Membership to View

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.