Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Run a macro with ABAP code

Former Member
0 Kudos
1,498

Hi everybody,

Could someone help me finding a way to run a macro of an excel sheet with an ABAP program.

Explanation:

- I launch an abap program that opened an excel sheet with that kind of code

-


include ole2incl.

DATA : w_excel type ole2_object,

w_books type ole2_object,

w_book type ole2_object,

w_sheets type ole2_object,

w_sheet type ole2_object,

w_cell type ole2_object,

w_rows type ole2_object,

w_font type ole2_object.

create object w_excel 'Excel.Application'.

CALL METHOD OF W_EXCEL 'WORKBOOKS' = W_BOOKS.

...

...

CALL METHOD OF W_SHEET 'CELLS' = W_CELL

exporting #1 = row

#2 = column.

....

....

CALL METHOD OF W_SHEET 'SAVEAS'.

....

....

-


With my ABAP program:

- i can open my excel template.

- i can write and format data inside my excel sheet.

- i can close and save my excel sheet.

but i don't how to run the macro that exists in my excel template.

Thank you in advance.

1 ACCEPTED SOLUTION

Sougata
Active Contributor
0 Kudos
220

Hello Loic,

You can give this a go:


CREATE OBJECT h_excel 'EXCEL.APPLICATION'.
Call Method of h_excel 'WORKBOOKS' = h_mapl.
set property of h_excel 'VISIBLE' = 0.
Call Method of h_mapl 'OPEN'
EXPORTING
#1 = d_file.
CALL METHOD OF H_EXCEL 'ActiveWorkbook' = h_book .
CALL METHOD OF H_book 'Activesheet' = H_sheet .

CALL METHOD OF h_excel 'RUN'
EXPORTING
#1 = ld_macro.

Hope it works for you and don't forget to reward points.

Cheers,

Sougata.

6 REPLIES 6

Former Member
0 Kudos
220

Hi,

Executing Excel Macro from SAP ABAP

I am populating data in excel sheet using OLE. This excel document contains Macro.

Is there any way I can start execution of Macro as soon as data is populated in Excel sheet.

If you are using OLE to populate the Excel Sheet then, then yes you can run a macro.

CALL METHOD OF OBJ_EX_APP 'Run'

EXPORTING #1 = MACROSTR.

Adding a Field Exit

To add a field exit first find the data element that is linked to the screen field- the screen number and program

Run program RSMODPRF. You will need to create two function modules one FIELD_EXIT_MEPO_VENDOR and

FIELD_EXIT_MEPO_VENDOR_A

FIELD_EXIT_MEPO_VENDOR should have code simply of input = ouput.

FIELD_EXIT_MEPO_VENDOR_A should have the code for whatever the functionality is required

Go back to RSMODPRF and run the program without parameters. Use the push buttons to assign the screen, 'A' and program. Use the drop down to activate the field list.

This should now work, but there is no way of debugging. For further information look at OSS 29377

program zzdirlist.

  • for AIX this method also works.

DATA: BEGIN OF TABL OCCURS 0,

TEXT(80) TYPE C,

END OF TABL.

DATA: COMMAND(256) TYPE C.

COMMAND = 'ls'.

CALL FUNCTION 'RFC_REMOTE_PIPE' DESTINATION 'SERVER_EXEC'

EXPORTING COMMAND = COMMAND

READ = 'X'

TABLES PIPEDATA = TABL.

LOOP AT TABL.

WRITE:/ TABL-text.

ENDLOOP.

Open the following link,

http://www.sap-img.com/abap/questions-about-bar-code-printing-in-sap.htm

Regards,

Jagadish.

Sougata
Active Contributor
0 Kudos
221

Hello Loic,

You can give this a go:


CREATE OBJECT h_excel 'EXCEL.APPLICATION'.
Call Method of h_excel 'WORKBOOKS' = h_mapl.
set property of h_excel 'VISIBLE' = 0.
Call Method of h_mapl 'OPEN'
EXPORTING
#1 = d_file.
CALL METHOD OF H_EXCEL 'ActiveWorkbook' = h_book .
CALL METHOD OF H_book 'Activesheet' = H_sheet .

CALL METHOD OF h_excel 'RUN'
EXPORTING
#1 = ld_macro.

Hope it works for you and don't forget to reward points.

Cheers,

Sougata.

Former Member
0 Kudos
220

Hi Sougata,

I saw your responce on this... Can you please help me with sample code for this requirement...

1. from ABAP program i would like to call Excel

2. Place Macro code in Excel

or

3. Calling Excel template which contains Macro

4. Run the macro from ABAP program.

Thanks for your help

Regards,

Srinivas

Former Member
0 Kudos
220

CALL METHOD document->execute_macro

EXPORTING macro_string = macro_string

param_count = param_count

script_name = script_name

param1 = param1

param2 = param2

param3 = param3

param4 = param4

param5 = param5

param6 = param6

param7 = param7

param8 = param8

param9 = param9

param10 = param10

param11 = param11

param12 = param12

no_flush = no_flush

IMPORTING error = error

retcode = retcode

CHANGING error_string = error_string

retvalue = retvalue.

Former Member
0 Kudos
220

Hi everybody,

i would like to thank you for your help.

I've just tried the code given by Sougata and it works very well.

It was exactly what i'm looking for.

Thanks equally to Swastik and Jagadish, i know that your code could be useful to me.

0 Kudos
220

Hello Loic,

I saw your query and sucess on this... Can you please help me with sample code for this requirement...

1. from ABAP program i would like to call Excel

2. Place Macro code in Excel

or

3. Calling Excel template which contains Macro

4. Run the macro from ABAP program.

Thanks for your help

Regards,

Srinivas