cancel
Showing results for 
Search instead for 
Did you mean: 

edit xlsm file and keep macro

almedin_hodzic53
Participant
0 Kudos
155

I have xlsm file that I want to fill but keep the macro which is inside file already.

I have abap2xlsx library on my system, but it deletes macro anyway. Any help would be useful.

View Entire Topic
remi-kaimal
Explorer
0 Kudos

Thats correct - the standard abap2xlsx library does not support writing to .xlsm files while preserving existing macros, as it is primarily designed for .xlsx format and does not retain VBA macros.

There are 2 possibe options
1) Option : Use abap2xlsx for Data & Manually Reattach Macros
Generate your .xlsx file using abap2xlsx.
Open it in Excel, reattach the macro manually, and save it as .xlsm.

2) Option : Read & Modify via ABAP and Preserve Macros
DATA: lo_excel TYPE OLE2_OBJECT,
lo_workbooks TYPE OLE2_OBJECT,
lo_workbook TYPE OLE2_OBJECT,
lo_sheet TYPE OLE2_OBJECT.

CREATE OBJECT lo_excel 'EXCEL.APPLICATION'.
SET PROPERTY OF lo_excel 'Visible' = 0. " Make Excel invisible

* Open existing xlsm file
GET PROPERTY OF lo_excel 'Workbooks' = lo_workbooks.
CALL METHOD OF lo_workbooks 'Open'
EXPORTING #1 = 'C:\path\to\your\file.xlsm'.

* Get active sheet
GET PROPERTY OF lo_excel 'ActiveSheet' = lo_sheet.

* Write data (Example: A1 = "Hello World")
SET PROPERTY OF lo_sheet 'Cells(1,1).Value' = 'Hello World'.

* Save the file keeping macros
CALL METHOD OF lo_workbook 'Save'.

* Close everything
CALL METHOD OF lo_workbook 'Close'.
CALL METHOD OF lo_excel 'Quit'.

FREE OBJECT lo_sheet.
FREE OBJECT lo_workbook.
FREE OBJECT lo_workbooks.
FREE OBJECT lo_excel.

almedin_hodzic53
Participant
0 Kudos

Thanks for the response. I think I need first approach, because I am downloading file from Fiori app.

I have a template stored as MIME on MIME repository, and that file is xlsm and contains macro. 

I want to fill that file with the data, but keep macro after editing.

Do you think this is possible with the first approach?