Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

SaveAs in excel using OLE doesn't work


I'm trying to create an excel, using OLE.

Excel opens up just fine, with the correct number of sheets with data in the columns. But it doesn''t save although sy-subrc = 0 after the SaveAs method is called.

Could you please have a look at my code, and try and help me out - Thanks!



DATA: file(255) TYPE c VALUE 'c:\test1.xls'.

 LOOP AT gt_excel INTO gs_excel.

*   Create new sheet
    ON CHANGE OF gs_excel-sheet_name.

*--For the first loop, Excel is initiated and one new sheet is added
      IF sy-tabix = 1 .
        SET PROPERTY OF excel 'Visible' = 1 .
        GET PROPERTY OF excel 'Workbooks' = workbook.

        GET PROPERTY OF workbook 'Application' = application .
        SET PROPERTY OF application 'SheetsInNewWorkbook' = 1 .
        CALL METHOD OF workbook 'Add' = wbook .
        GET PROPERTY OF application 'ActiveSheet' = activesheet .
        SET PROPERTY OF activesheet 'Name' = gs_excel-sheet_name.
*--For the rest of loops, other sheets are added
      ELSE .
        GET PROPERTY OF wbook 'Sheets' = sheets .
        CALL METHOD OF sheets 'Add' = newsheet .
        SET PROPERTY OF newsheet 'Name' = gs_excel-sheet_name.
        g_row = 0.


    g_row = g_row + 1.
    g_col = 1.

    CALL METHOD OF excel 'cells' = cells EXPORTING #1 = g_row #2 = g_col.
    SET PROPERTY OF cells 'value' = gs_excel-col1.
    SET PROPERTY OF cells 'ColumnWidth' = '90'.
     g_col = g_col + 1.
    CALL METHOD OF excel 'cells' = cells EXPORTING #1 = g_row #2 = g_col.
    SET PROPERTY OF cells 'value' = gs_excel-col2.
    SET PROPERTY OF cells 'ColumnWidth' = '30'.

* save excel speadsheet
  CALL METHOD OF workbook 'SaveAs' EXPORTING #1 = file.
  CALL METHOD OF workbook 'Close'.
  CALL METHOD OF excel 'Quit'.

  FREE OBJECT: application, workbook, sheets, sheet, cells, interior.
  FREE OBJECT: excel, wbook, activesheet, newsheet              .

Former Member
Not what you were looking for? View more on this topic or Ask a question