Skip to Content
0
Former Member
Feb 03, 2005 at 06:57 PM

Running Excel macros from ABAP

1371 Views

Hello everyone,

I am trying to execute an Excel macro from an ABAP program. We are currently on a 46C system. While doing some research on help.sap.com I came across the method execute_macro in class i_oi_document_proxy. I’ve never used methods in ABAP before and I’m not really sure what I’m doing. Has anyone got this to work? When I try to run the program it dumps with error OBJECTS_OBJREF_NOT_ASSIGNED.

Thanks,

Becky

Here is the program:

REPORT ztest_program.

INCLUDE ole2incl.

DATA gs_excel TYPE ole2_object .

DATA gs_wbooks TYPE ole2_object .

DATA gs_wbook TYPE ole2_object .

DATA gs_application TYPE ole2_object .

DATA: h_sheet TYPE ole2_object.

DATA: document TYPE REF TO i_oi_document_proxy.

*Name of the macro in Excel

DATA: macro_string(50) TYPE c

VALUE 'FB03process.FromTheBeginning',

no_flush TYPE c,

param_count TYPE i VALUE 0,

script_name TYPE c VALUE 'X',

error TYPE REF TO i_oi_error

OCCURS 0 WITH HEADER LINE,

retcode TYPE soi_ret_string,

error_string(50) TYPE c,

retvalue(30) TYPE c.

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'

EXPORTING

text = text-007

EXCEPTIONS

OTHERS = 1.

CREATE OBJECT gs_excel 'EXCEL.APPLICATION' .

SET PROPERTY OF gs_excel 'Visible' = 1 .

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 = 'D:\temp\FB03process.xls' .

  • tell user what is going on

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'

EXPORTING

  • PERCENTAGE = 0

text = text-009

EXCEPTIONS

OTHERS = 1.

GET PROPERTY OF gs_excel 'ACTIVESHEET' = h_sheet.

CALL METHOD document->execute_macro

EXPORTING macro_string = macro_string

param_count = param_count

script_name = script_name

no_flush = no_flush

IMPORTING error = error

retcode = retcode

CHANGING error_string = error_string

retvalue = retvalue.

  • disconnect from Excel

FREE OBJECT gs_excel.

PERFORM err_hdl.

FORM err_hdl.

IF sy-subrc <> 0.

WRITE: / 'Fehler bei OLE-Automation:'(010), sy-subrc.

STOP.

ENDIF.

ENDFORM. " ERR_HDL