ABAP as OLE2 Automation Controller

More and more desltop applications use the automation interface to make their functions available to other applications or script languages. All of the operations available to users via the keyboard and mouse can also be carried out by an external program or script. You can control such desktop applications (for example, WORD or EXCEL from an ABAP program using the following statements:

CREATE OBJECT

SET PROPERTY

GET PROPERTY

CALL METHOD

FREE OBJECT

Conversion Rules

When parameters are passed, they are always converted by character:

ABAP type <-- ABAP norm --> CHAR -- OLE norm --> OLE type
according to their type info.

If there is no type infor, or the OLE type is undefined (type
variant), the OLE type is set according to the ABAP type
as follows:

ABAP Types OLE Type
I Int
P, F Double
D Date
T Time
other Character

The maximum length of a character variable is currently restricted to 255.
Note that the semantics of the paramters for certain objects depend on the corresponding OLE type.

The Automation Queue

Communication between the application server and the frontend uses the RFC interface. The SAPgui contains special functions for OLE, which you can call from ABAP using normal RFC calls (with destination 'SAPGUI'). To improve performance, all automation commands are buffered in queue before being sent to the frontend in one batch. The moment at which the queue is sent to the frontend can be determined by the programmer in one of two ways:

1.) You can let the system decide when to transmit the queue. Here, you use all of the automation statements without the NO FLUSH addition. As soon as a statement occurs that is not one of CREATE OBJECT, CALL METHOD OF , SET PROPERTY, GET PROPERTY, or FREE OBJECT, the SAP Automation Controller forwards the queue to the frontend. All of the variables that are addressed in automation statements are filled with new values after the queue has been processed.

Example:

data: myexcel type ole2_object, workbooks type ole2_object.

data: filename(10) type c.

create object 'EXCEL.APPLICATION' excel.                      "1
call method of myexcel 'WORKBOOKS' = workbooks.               "2
filename = 'TEST.XLS'.                                        "3
call method of workbooks 'OPEN' exporting #1 = filename.      "4

Before line 3 of the program is executed, the automation queue is sent to the frontend.

2.) You use the addition NO FLUSH in the automation statements. This switches off the automatic transmission of the queue to the frontend. You are then responsible for transmitting the queue to the frontend yourself by calling the functino module 'FLUSH', which forces the synchronization. In this case, you must also ensure that the variable contents are used correctly. If you address a variable before a flush, it may contain an incorrect value.

Example:

data: myexcel type ole2_object, workbooks type ole2_object.
data: filename(10) type c.

create object 'EXCEL.APPLICATION' excel no flush.             "1
call method of myexcel 'WORKBOOKS' = workbooks no flush.      "2
filename = 'TEST.XLS'.                                       "3
call method of workbooks 'OPEN' exporting #1 = filename no flush. "4
call function 'flush'.                                        "5

The automation queue is not sent to the frontend until the function module 'FLUSH' is explicitly called in line 5.