Skip to Content
0
Former Member
Dec 02, 2015 at 02:38 PM

Excel is locked when download by using OLE

1810 Views

Hi Experts - I am adding a new sheet to existing excel placed on Desktop.I want to add a few information on New sheet.Successfully added new sheet to existing sheet, but when i open the the Excel it is saying " .xls is already open. Reopening will cause any changes you made to be discarded.Do you want to reopen". Once i opened new Sheet changes are not there?Below is my code.Appreciate your help.

DATA gs_excel TYPE ole2_object .

DATA gs_wbooks TYPE ole2_object .

DATA gs_wbook TYPE ole2_object .

DATA: gs_application TYPE ole2_object,

sheets TYPE ole2_object,

sheet TYPE ole2_object,

first_sheet TYPE ole2_object,

cells TYPE ole2_object.

CREATE OBJECT gs_excel 'EXCEL.APPLICATION'.

SET PROPERTY OF gs_excel 'Visible' = 0.

GET PROPERTY OF gs_excel 'Workbooks' = gs_wbooks .

* GET PROPERTY OF gs_wbooks 'Application' = gs_application .

*--Opening the existing document

CALL METHOD OF gs_wbooks 'Open' = gs_wbook

EXPORTING #1 = p_path.

IF sy-subrc = 0.

*create first Excel sheet

CALL METHOD OF gs_excel 'Worksheets' = first_sheet

EXPORTING #1 = 1.

CALL METHOD OF gs_excel 'Worksheets' = sheets.

CALL METHOD OF sheets 'Add'

EXPORTING

#1 = first_sheet.

GET PROPERTY OF gs_excel 'ActiveSheet' = sheet.

SET PROPERTY OF sheet 'Name' = 'Header'.

* index = row_max * ( sy-tabix - 1 ) + 1. " 1 - column name

CALL METHOD OF sheet 'Cells' = cells EXPORTING #1 = 1. "index.

SET PROPERTY OF cells 'Value' = 'Cell data1'.

CALL METHOD OF sheet 'Cells' = cells EXPORTING #1 = 2. "index.

SET PROPERTY OF cells 'Value' = 'Cell data2'.

* Save excel speadsheet to particular filename

CALL METHOD OF sheets 'Save'

EXPORTING

#1 = p_path "filename

#2 = 1.

CALL METHOD OF gs_excel 'QUIT'.

FREE OBJECT: gs_excel, gs_wbooks, gs_wbook, sheets, sheet.