Sie sind auf Seite 1von 18

An Easy Reference For OLE

Automation




SDN Community Contribution
(This is not an official SAP document.)


Disclaimer & Liability Notice
This document may discuss sample coding or other information that does not include SAP official
interfaces and therefore is not supported by SAP. Changes made based on this information are not
supported and can be overwritten during an upgrade.
SAP will not be held liable for any damages caused by using or misusing the information, code or
methods suggested in this document, and anyone using these methods does so at his/her own risk.
SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the
content of this technical article or code sample, including any liability resulting from incompatibility
between the content within this document and the materials and services offered by SAP. You agree
that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this
document.
An Easy Reference For OLE
Automation






An
Easy Reference
for
OLE Automation
(MS Word & MS Excel)






































Serdar MEKLER
2004, Ankara TURKEY





An Easy Reference for OLE Automation


Table of Contents

SDN Community Contribution.......................................................................................................... 1
Disclaimer & Liability Notice........................................................................................................... 1
Table of Contents............................................................................................................................... 1
Purpose............................................................................................................................................... 2
Prerequisites....................................................................................................................................... 2
A Basics......................................................................................................................................... 2
A.1 Data Definitions................................................................................................................. 2
A.2 Creating an OLE Object..................................................................................................... 2
A.3 Calling a Method of an Object........................................................................................... 3
A.4 Setting a Property of an Object.......................................................................................... 3
A.5 Getting a Property of an Object......................................................................................... 3
A.6 Freeing an Object............................................................................................................... 3
A.7 NO FLUSH Addition......................................................................................................... 3
A.8 Knowing About Methods and Properties of an OLE Object............................................. 4
B A General Scheme for Integration with MS Word.................................................................... 5
C A General Scheme for Integration with MS Excel .................................................................... 9
D Conclusion............................................................................................................................... 16
Author Bio....................................................................................................................................... 16


1
An Easy Reference for OLE Automation

Purpose

Purpose of this tutorial is to provide a step by step guide illustrating usage of OLE
automation techniques within an ABAP program. It is recommended to use SAP DOI (Desktop
Office Integration) for office integration since it standardizes the procedure, handles the integration
by a structured and robust service. However, for some cases, developers need some simpler and
more flexible way. This tutorial does not aim to dive into profound technical facts about the
underlying technology. It does not cover all related details about the topic, either. But, this tutorial
may be utilized as a quick reference-manual since it aims to draw a general scheme.

Prerequisites
Obviously, a basic level ABAP programming skill is required to make use of this tutorial.
Knowledge of the macro language of the application will be of great help and some knowledge
about the OLE technology is recommended.


A Basics
Basically, utilizing OLE automation is achieved by creating OLE objects and calling their
methods. Within ABAP, five basic statements are used for OLE automation. So, this first section
will deal with those ABAP statements.

A.1 Data Definitions
For each entity of the OLE object, there must be a variable holding handle data for it. These
handle variables should be of the type ol e2_obj ect which is defined in the type-pool ol e2.
Hence, within your program you should include ol e2i ncl which wraps the pool and then define
your handle variables.
Code Part A.1 Data definitions
*- - I ncl ude f or OLE- enabl i ng def i ni t i ons
I NCLUDE ol e2i ncl .

*- - Gl obal var i abl es
*- - Var i abl es t o hol d OLE obj ect handl es
DATA gs_wor d TYPE ol e2_obj ect .
. . . .
A.2 Creating an OLE Object
To create an OLE object, the ABAP statement CREATE OBJ ECT is used.
Its syntax is: CREATE OBJECT obj class.
Here, obj is the handle variable for the base object and class is the specific identifier
for the corresponding application.
e.g. CREATE OBJ ECT gs_wor d ' WORD. APPLI CATI ON' .

If the creation is successful the value of sy- subr c becomes 0, otherwise it becomes
some other value (i.e. 1, 2 or 3 with respect to the error type).


2
An Easy Reference for OLE Automation

A.3 Calling a Method of an Object
After creating an OLE object, it is possible to call its methods to execute its functionality.
This is achieved by using the ABAP statement CALL METHOD OF. You can also pass required
parameters using this statement.
The syntax is: CALL METHOD OF obj m [= f] [EXPORTING p1 = f1 ... pn = fn] .
Here, obj is the object handle variable, m is the method name, f is the variable where
the output of the method will be replaced and pn = fn assignments are used to pass parameters.
The EXPORTING part must be at the end of the statement. For the moment, parameter passing is
done by giving their positions and the corresponding value.
e.g. CALL METHOD OF gs_wor d ' Document s' = gs_document s .
CALL METHOD OF gs_sel ect i on ' TypeText ' EXPORTI NG #1 = i p_t ext .

Successful method calls makes sy- subr c value 0, and unsuccessful cases make it some
other value.
A.4 Setting a Property of an Object
To set a property of an OLE object, the ABAP statement SET PROPERTY OF is used.
The syntax is: SET PROPERTY OF obj p = f .
Here, obj is the object handle variable, p is the property name and f is the value to
be assigned.
e.g. SET PROPERTY OF gs_wor d ' Vi si bl e' = 1 .

Operation result status is indicated at the system variable sy- subr c; 0 for successful
operations and another value for erroneous cases.
A.5 Getting a Property of an Object
Getting the value of a property of an OLE object is obviously similar to setting it. For this,
the ABAP statement GET PROPERTY OF is used.
The syntax is: GET PROPERTY OF obj p = f .
Here, obj is the object handle variable, p is the property name and f is the variable
to which the value of the property is assigned.
e.g. GET PROPERTY OF gs_vi ew ' Type' = gv_vi ewt ype .

Again, operation result status is indicated at the system variable sy- subr c; 0 for
successful operations and another value for erroneous cases.
A.6 Freeing an Object
Generally for performance issues, it is required to free the memory allocated for OLE
objects. For this, the ABAP statement FREE OBJ ECT is used.
The syntax is: FREE OBJECT obj. where obj is the object handle variable.

A.7 NO FLUSH Addition
Normally, OLE statements are buffered by the ABAP processor and executed at the
frontend collectively before the first statement which is not of OLE context. Using this addition
prevents this and postpones the execution till just before the first non-OLE statement coming after
an OLE statement without NO FLUSH addition.

3
An Easy Reference for OLE Automation

A.8 Knowing About Methods and Properties of an OLE Object
What a developer requires is generally to retrieve information about methods and
properties that the OLE object bestows. Generally, it is a useful way to use the macro debugging of
the application to figure out those. The relevant library of the application will also give useful
information about these. What we require to figure out is the class chain whose instances we will
create and make method calls. Recording and debugging a macro generally provides relevant
object hierarchy to be called within the program.
Here is a simple VB code:



Se l e c t i o n. Fo nt . Bo l d = Tr ue

Value to be set
Upper object


Attribute
Relevant Object


This line of macro code tells us that to set the attribute bold we must create OLE instances
for font and selection and then set the property bold of font object.
So lets switch to ABAP and write relevant code:
i. Getting instance for font:
GET PROPERTY OF gs_sel ect i on ' Font ' = gs_f ont .
ii. Setting attribute bold
SET PROPERTY OF gs_f ont ' Bol d' = ' 1' .

Here, it is seen that to retrieve lower level instances we use GET PROPERTY OF statement.
One will ask how to instantiate selection object which seems to be the topmost object although in
the whole picture it is not. This object is reached following the class hierarchy from the root OLE
object created for the application. This procedure is illustrated in code parts in following sections.


Here is another VB code line calling a method of an object:


Se l e c t i o n. Ty pe Te x t Te x t : = "Thus Spo ke Za r a t hus t r a "

Value to pass to
the parameter
Relevant object


Method Name Parameter Name


This VB code is adapted to ABAP as

CALL METHOD OF gs_sel ect i on ' TypeText '
EXPORTI NG
#1 = ' Thus Spoke Zar at hust r a' .



4
An Easy Reference for OLE Automation


B A General Scheme for Integration with MS Word
Now, it is time to build an application having integration with MS Word and using some of
its basic features. So, lets define the outline for its task as;
i. put a proper title
ii. write some text
iii. insert a table
iv. write a second snippet of text
v. insert some identification text at the header
vi. set measurement unit for the document to CM

To be clearer, the code will be written in a non-modular way which will repeat reusable
parts. For your further works, you can modularize all these. For example, all functional codes may
be written as subroutines to be collected in a subroutine pool altogether. Or a function group can
be implemented. In fact, the best way is to develop a class to encapsulate all.

Step 1 Data declarations
Code Part B.1 Data declarations
REPORT zol e_t ut or _exampl e_ms_wor d .

*- - I ncl ude f or OLE- enabl i ng def i ni t i ons
I NCLUDE ol e2i ncl .

*- - Gl obal var i abl es
*- - Var i abl es t o hol d OLE obj ect and ent i t y handl es
DATA gs_wor d TYPE ol e2_obj ect . " OLE obj ect handl e
DATA gs_document s TYPE ol e2_obj ect . " Document s
DATA gs_act doc TYPE ol e2_obj ect . " Act i ve document
DATA gs_appl i cat i on TYPE ol e2_obj ect . " Appl i cat i on
DATA gs_opt i ons TYPE ol e2_obj ect . " Appl i cat i on opt i ons
DATA gs_act wi n TYPE ol e2_obj ect . " Act i ve wi ndow
DATA gs_act pan TYPE ol e2_obj ect . " Act i ve pane
DATA gs_vi ew TYPE ol e2_obj ect . " Vi ew
DATA gs_sel ect i on TYPE ol e2_obj ect . " Sel ect i on
DATA gs_f ont TYPE ol e2_obj ect . " Font
DATA gs_par f or mat TYPE ol e2_obj ect . " Par agr aph f or mat
DATA gs_t abl es TYPE ol e2_obj ect . " Tabl es
DATA gs_r ange TYPE ol e2_obj ect . " Range handl e f or var i ous r anges
DATA gs_t abl e TYPE ol e2_obj ect . " One t abl e
DATA gs_t abl e_bor der TYPE ol e2_obj ect . " Tabl e bor der
DATA gs_cel l TYPE ol e2_obj ect . " One cel l of a t abl e
DATA gs_par agr aph TYPE ol e2_obj ect . " Par agr aph

DATA gv_pos( 5) TYPE n . " Posi t i on i nf or mat i on f or t abl e

Step 2 Creating the OLE object and get main entities to handle variables.


START- OF- SELECTI ON .

*- - Cr eat i ng OLE obj ect handl e var i abl e
CREATE OBJ ECT gs_wor d ' WORD. APPLI CATI ON' .
I F sy- subr c NE 0 .
MESSAGE s000( su) WI TH ' Er r or whi l e cr eat i ng OLE obj ect ! ' .
LEAVE PROGRAM .
ENDI F .

5
An Easy Reference for OLE Automation

Code Part B.2 Creating the OLE object

*- - Set t i ng obj ect ' s vi si bi l i t y pr oper t y
SET PROPERTY OF gs_wor d ' Vi si bl e' = ' 1' .
*- - Openi ng a new document
GET PROPERTY OF gs_wor d ' Document s' = gs_document s .
CALL METHOD OF gs_document s ' Add' .

*- - Get t i ng act i ve document handl e
GET PROPERTY OF gs_wor d ' Act i veDocument ' = gs_act doc .
*- - Get t i ng appl i cat i ons handl e
GET PROPERTY OF gs_act doc ' Appl i cat i on' = gs_appl i cat i on .

Step 3 Setting the measurement unit to CM
Code Part B.3 Setting measurement unit
*- - Set t i ng t he measur ement uni t
GET PROPERTY OF gs_appl i cat i on ' Opt i ons' = gs_opt i ons .
SET PROPERTY OF gs_opt i ons ' Measur ement Uni t ' = ' 1' . " CM

Step 4 Some header text
Code Part B.4 Setting header content
*- - Get t i ng handl e f or t he sel ect i on whi ch i s her e t he char act er at t he
*- - cur sor posi t i on
GET PROPERTY OF gs_appl i cat i on ' Sel ect i on' = gs_sel ect i on .
GET PROPERTY OF gs_sel ect i on ' Font ' = gs_f ont .
GET PROPERTY OF gs_sel ect i on ' Par agr aphFor mat ' = gs_par f or mat .

*- - Set t i ng f ont at t r i but es
SET PROPERTY OF gs_f ont ' Name' = ' Ar i al ' .
SET PROPERTY OF gs_f ont ' Si ze' = ' 10' .
SET PROPERTY OF gs_f ont ' Bol d' = ' 0' . " Not bol d
SET PROPERTY OF gs_f ont ' I t al i c' = ' 1' . " I t al i c
SET PROPERTY OF gs_f ont ' Under l i ne' = ' 0' . " Not under l i ned

*- - Set t i ng par agr aph f or mat at t r i but e
SET PROPERTY OF gs_par f or mat ' Al i gnment ' = ' 2' . " Ri ght - j ust i f i ed
CALL METHOD OF gs_sel ect i on ' TypeText '
EXPORTI NG
#1 = ' Thi s i s an OLE exampl e! ' .

*- - Set t i ng t he vi ew t o t he mai n document agai n
SET PROPERTY OF gs_vi ew ' SeekVi ew' = ' 0' . " Mai n document vi ew

Step 5 Writing the title
Code Part B.5 Writing the title
*- - Reset i ng f ont at t r i but es f or t he t i t l e
SET PROPERTY OF gs_f ont ' Name' = ' Ti mes New Roman' .
SET PROPERTY OF gs_f ont ' Si ze' = ' 16' .
SET PROPERTY OF gs_f ont ' Bol d' = ' 1' . " Bol d
SET PROPERTY OF gs_f ont ' I t al i c' = ' 0' . " Not I t al i c
SET PROPERTY OF gs_f ont ' Under l i ne' = ' 0' . " Not under l i ned

*- - Set t i ng par agr aph f or mat at t r i but e
SET PROPERTY OF gs_par f or mat ' Al i gnment ' = ' 1' . " Cent er ed
CALL METHOD OF gs_sel ect i on ' TypeText '
EXPORTI NG
#1 = t ext - 000.
*- - Advanci ng cur sor t o t he new l i ne
CALL METHOD OF gs_sel ect i on ' TypePar agr aph' .




6
An Easy Reference for OLE Automation

Step 6 Writing some text
Code Part B.6 Writing some text
*- - Reset i ng f ont at t r i but es f or or di nar y t ext
SET PROPERTY OF gs_f ont ' Name' = ' Ti mes New Roman' .
SET PROPERTY OF gs_f ont ' Si ze' = ' 12' .
SET PROPERTY OF gs_f ont ' Bol d' = ' 0' . " Not bol d
SET PROPERTY OF gs_f ont ' I t al i c' = ' 0' . " Not I t al i c
SET PROPERTY OF gs_f ont ' Under l i ne' = ' 0' . " Not under l i ned

*- - Set t i ng par agr aph f or mat at t r i but e
SET PROPERTY OF gs_par f or mat ' Al i gnment ' = ' 3' . " J ust i f i ed
CALL METHOD OF gs_sel ect i on ' TypeText '
EXPORTI NG
#1 = t ext - 001.

*- - Ski p some l i nes
DO 4 TI MES .
CALL METHOD OF gs_sel ect i on ' TypePar agr aph' .
ENDDO .


Step 7 Inserting a table and filling some of its cells

*- - Get t i ng ent i t y handl es f or t he ent i t i es on t he way
GET PROPERTY OF gs_act doc ' Tabl es' = gs_t abl es .
GET PROPERTY OF gs_sel ect i on ' Range' = gs_r ange .
*- - Addi ng a t abl e wi t h 3 r ows and 2 col umns
CALL METHOD OF gs_t abl es ' Add' = gs_t abl e
EXPORTI NG
#1 = gs_r ange " Handl e f or r ange ent i t y
#2 = ' 3' " Number of r ows
#3 = ' 2' . " Number of col umns
*- - Set t i ng bor der at t r i but e f or t he t abl e
GET PROPERTY OF gs_t abl e ' Bor der s' = gs_t abl e_bor der .
SET PROPERTY OF gs_t abl e_bor der ' Enabl e' = ' 1' . " Wi t h bor der

*- - Fi l l i ng t he t abl e wi t h dummy dat a
*- - Reset i ng f ont at t r i but es f or t abl e cont ent
SET PROPERTY OF gs_f ont ' Name' = ' Gar amond' .
SET PROPERTY OF gs_f ont ' Si ze' = ' 11' .
SET PROPERTY OF gs_f ont ' Bol d' = ' 0' . " Not bol d
SET PROPERTY OF gs_f ont ' I t al i c' = ' 0' . " Not I t al i c
SET PROPERTY OF gs_f ont ' Under l i ne' = ' 0' . " Not under l i ned

*- - Get t i ng cel l coor di nat es
CALL METHOD OF gs_t abl e ' Cel l ' = gs_cel l
EXPORTI NG
#1 = ' 1' " f i r st r ow
#2 = ' 1' . " f i r st col umn

*- - Get t i ng t he r ange handl e t o wr i t e t he t ext
GET PROPERTY OF gs_cel l ' Range' = gs_r ange .
*- - Fi l l i ng t he cel l
SET PROPERTY OF gs_r ange ' Text ' = ' OLE' .

*- - Get t i ng cel l coor di nat es
CALL METHOD OF gs_t abl e ' Cel l ' = gs_cel l
EXPORTI NG
#1 = ' 3' " t hi r d r ow
#2 = ' 2' . " second col umn

*- - Get t i ng t he r ange handl e t o wr i t e t he t ext


7
An Easy Reference for OLE Automation

Code Part B.7 Some table work
GET PROPERTY OF gs_cel l ' Range' = gs_r ange .
*- - Fi l l i ng t he cel l
SET PROPERTY OF gs_r ange ' Text ' = ' OLE' .


*- - Advanci ng t he cur sor t o t he end of t he t abl e
GET PROPERTY OF gs_t abl e ' Range' = gs_r ange .
GET PROPERTY OF gs_r ange ' End' = gv_pos .
SET PROPERTY OF gs_r ange ' St ar t ' = gv_pos .
CALL METHOD OF gs_r ange ' Sel ect ' .

*- - Ski p some l i nes
DO 3 TI MES .
CALL METHOD OF gs_sel ect i on ' TypePar agr aph' .
ENDDO .


Step 8 Adding some other text and indent its paragraph
Code Part B.8 Writing some indented text
*- - Reset i ng f ont at t r i but es f or or di nar y t ext
SET PROPERTY OF gs_f ont ' Name' = ' Ti mes New Roman' .
SET PROPERTY OF gs_f ont ' Si ze' = ' 12' .
SET PROPERTY OF gs_f ont ' Bol d' = ' 0' . " Not bol d
SET PROPERTY OF gs_f ont ' I t al i c' = ' 0' . " Not I t al i c
SET PROPERTY OF gs_f ont ' Under l i ne' = ' 0' . " Not under l i ned

*- - Set t i ng par agr aph f or mat at t r i but e
SET PROPERTY OF gs_par f or mat ' Al i gnment ' = ' 3' . " J ust i f i ed
*- - I ndent t he par agr aph once
GET PROPERTY OF gs_sel ect i on ' Par agr aphs' = gs_par agr aph .
CALL METHOD OF gs_par agr aph ' I ndent ' .
CALL METHOD OF gs_sel ect i on ' TypeText '
EXPORTI NG
#1 = t ext - 002.

Step 9 Freeing object handle variable to deallocate memory
Code Part B.9 Freeing object handle variable
FREE OBJ ECT gs_wor d .

Result:















8
An Easy Reference for OLE Automation

C A General Scheme for Integration with MS Excel
Secondly, lets build an application having integration with MS Excel and using some of its
basic features. So, lets define the outline for its task as;
i. User inputs the number of worksheets
ii. For each sheet, creates some data to be also the source for a chart
iii. Makes some cell formatting
iv. Draws the chart and relocates it to the proper place on the sheet

To be clearer again, the code will be written in a non-modular way which will repeat
reusable parts. For your further works, you can modularize all these. For example, all functional
codes may be written as subroutines to be collected in a subroutine pool altogether. Or a function
group can be implemented. In fact, the best way is to develop a class to encapsulate all. The order
of method calls is important, so do not change their order.

Step 1 Data declarations
Code Part C.1 Data declarations
REPORT zol e_t ut or _exampl e_ms_excel .

I NCLUDE ol e2i ncl .

DATA: gs_excel TYPE ol e2_obj ect ,
gs_wbookl i st TYPE ol e2_obj ect ,
gs_appl i cat i on TYPE ol e2_obj ect ,
gs_wbook TYPE ol e2_obj ect ,
gs_act i vesheet TYPE ol e2_obj ect ,
gs_sheet s TYPE ol e2_obj ect ,
gs_newsheet TYPE ol e2_obj ect ,
gs_cel l 1 TYPE ol e2_obj ect ,
gs_cel l 2 TYPE ol e2_obj ect ,
gs_cel l s TYPE ol e2_obj ect ,
gs_r ange TYPE ol e2_obj ect ,
gs_f ont TYPE ol e2_obj ect ,
gs_i nt er i or TYPE ol e2_obj ect ,
gs_col umns TYPE ol e2_obj ect ,
gs_char t s TYPE ol e2_obj ect ,
gs_char t TYPE ol e2_obj ect ,
gs_char t t i t l e TYPE ol e2_obj ect ,
gs_char t t i t l echar TYPE ol e2_obj ect ,
gs_char t obj ect s TYPE ol e2_obj ect .


DATA gv_sheet _name( 20) TYPE c .
DATA gv_out er _i ndex LI KE sy- i ndex .
DATA gv_i nt ex( 2) TYPE c .
DATA gv_l i ne_cnt r TYPE i . " l i ne count er
DATA gv_l i nno TYPE i . " l i ne number
DATA gv_col no TYPE i . " col umn number
DATA gv_val ue TYPE i . " dat a

PARAMETERS: p_sheet s TYPE i .








9
An Easy Reference for OLE Automation


Step 2 Initiate the do-loop and OLE automation base objects
Code Part C.2 Looping and initializing, adding new worksheets
START- OF- SELECTI ON .

DO p_sheet s TI MES .

*- - For mi ng sheet name
gv_i nt ex = sy- i ndex .
gv_out er _i ndex = sy- i ndex .
CONCATENATE ' Excel Sheet #' gv_i nt ex I NTO gv_sheet _name .

*- - For t he f i r st l oop, Excel i s i ni t i at ed and one new sheet i s added
I F sy- i ndex = 1 .
CREATE OBJ ECT gs_excel ' EXCEL. APPLI CATI ON' .
SET PROPERTY OF gs_excel ' Vi si bl e' = 1 .
GET PROPERTY OF gs_excel ' Wor kbooks' = gs_wbookl i st .
GET PROPERTY OF gs_wbookl i st ' Appl i cat i on' = gs_appl i cat i on .
SET PROPERTY OF gs_appl i cat i on ' Sheet sI nNewWor kbook' = 1 .
CALL METHOD OF gs_wbookl i st ' Add' = gs_wbook .

GET PROPERTY OF gs_appl i cat i on ' Act i veSheet ' = gs_act i vesheet .
SET PROPERTY OF gs_act i vesheet ' Name' = gv_sheet _name .
*- - For t he r est of l oops, ot her sheet s ar e added
ELSE .
GET PROPERTY OF gs_wbook ' Sheet s' = gs_sheet s .
CALL METHOD OF gs_sheet s ' Add' = gs_newsheet .
SET PROPERTY OF gs_newsheet ' Name' = gv_sheet _name .
ENDI F .

gv_l i ne_cnt r = 1 . " l i ne count er


Step3 Write the title and format it

*- - Ti t l e
*- - Sel ect i ng cel l ar ea t o be mer ged.
CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 1
EXPORTI NG
#1 = 1
#2 = 1.
CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 2
EXPORTI NG
#1 = 1
#2 = 4.

CALL METHOD OF gs_excel ' Range' = gs_cel l s
EXPORTI NG
#1 = gs_cel l 1
#2 = gs_cel l 2.
CALL METHOD OF gs_cel l s ' Sel ect ' .

*- - Mer gi ng
CALL METHOD OF gs_cel l s ' Mer ge' .

*- - Set t i ng t i t l e dat a
CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 1
EXPORTI NG
#1 = gv_l i ne_cnt r
#2 = 1.

SET PROPERTY OF gs_cel l 1 ' Val ue' = ' TI TLE' .

10
An Easy Reference for OLE Automation


Code Part C.3 Writing and formatting the title

*- - For mat t i ng t he t i t l e
GET PROPERTY OF gs_cel l 1 ' Font ' = gs_f ont .
SET PROPERTY OF gs_f ont ' Under l i ne' = 2 .
SET PROPERTY OF gs_f ont ' Bol d' = 1 .
SET PROPERTY OF gs_cel l 1 ' Hor i zont al Al i gnment ' = - 4108 .
GET PROPERTY OF gs_cel l 1 ' I nt er i or ' = gs_i nt er i or .
SET PROPERTY OF gs_i nt er i or ' Col or I ndex' = 15 .
SET PROPERTY OF gs_i nt er i or ' Pat t er n' = - 4124 .
SET PROPERTY OF gs_i nt er i or ' Pat t er nCol or I ndex' = - 4105 .


Step 4 Write some additional data for the title area and format them

gv_l i ne_cnt r = gv_l i ne_cnt r + 1 .

*- - Wr i t i ng some addi t i onal dat a f or t he t i t l e
CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 1
EXPORTI NG
#1 = gv_l i ne_cnt r
#2 = 1.

SET PROPERTY OF gs_cel l 1 ' Val ue' = ' Sheet No' .

CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 1
EXPORTI NG
#1 = gv_l i ne_cnt r
#2 = 5.

SET PROPERTY OF gs_cel l 1 ' Val ue' = ' : ' .

CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 1
EXPORTI NG
#1 = gv_l i ne_cnt r
#2 = 6.

SET PROPERTY OF gs_cel l 1 ' Val ue' = gv_i nt ex .

*- - For mat t i ng t he ar ea of addi t i onal dat a 1
CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 1
EXPORTI NG
#1 = 1
#2 = 1.
CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 2
EXPORTI NG
#1 = gv_l i ne_cnt r
#2 = 5.

CALL METHOD OF gs_excel ' Range' = gs_cel l s
EXPORTI NG
#1 = gs_cel l 1
#2 = gs_cel l 2.
CALL METHOD OF gs_cel l s ' Sel ect ' .

GET PROPERTY OF gs_cel l s ' Font ' = gs_f ont .
SET PROPERTY OF gs_f ont ' Bol d' = 1 .





11
An Easy Reference for OLE Automation

Code Part C.4 Some additional writing to the title area, formatting and bordering around the title area
*- - For mat t i ng t he ar ea of addi t i onal dat a 2
CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 1
EXPORTI NG
#1 = 1
#2 = 5.
CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 2
EXPORTI NG
#1 = gv_l i ne_cnt r
#2 = 5.

CALL METHOD OF gs_excel ' Range' = gs_cel l s
EXPORTI NG
#1 = gs_cel l 1
#2 = gs_cel l 2.
CALL METHOD OF gs_cel l s ' Sel ect ' .

GET PROPERTY OF gs_cel l s ' Col umns' = gs_col umns .
CALL METHOD OF gs_col umns ' Aut oFi t ' .

*- - Bor der i ng t i t l e dat a ar ea
CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 1
EXPORTI NG
#1 = 1
#2 = 1.
CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 2
EXPORTI NG
#1 = gv_l i ne_cnt r
#2 = 6.

CALL METHOD OF gs_excel ' Range' = gs_cel l s
EXPORTI NG
#1 = gs_cel l 1
#2 = gs_cel l 2.
CALL METHOD OF gs_cel l s ' Sel ect ' .

CALL METHOD OF gs_cel l s ' Bor der Ar ound'
EXPORTI NG
#1 = 1 " cont i nuous l i ne
#2 = 4. " t hi ck



Step 5 Put axis labels to the data area
Code Part C.5 Axis Labels
*- - Put t i ng axi s l abel s
gv_col no = 2 .
gv_l i ne_cnt r = gv_l i ne_cnt r + 5 .
gv_l i nno = gv_l i ne_cnt r - 1 .


CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 1
EXPORTI NG
#1 = gv_l i nno
#2 = 1.

SET PROPERTY OF gs_cel l 1 ' Val ue' = ' X' .

CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 1
EXPORTI NG
#1 = gv_l i ne_cnt r
#2 = 1.

SET PROPERTY OF gs_cel l 1 ' Val ue' = ' Y' .


12
An Easy Reference for OLE Automation

Step 6 Generate some data
Code Part C.6 Generating Data
*- - Gener at i ng some dat a
DO 3 TI MES .
gv_val ue = gv_out er _i ndex * sy- i ndex * 10 .

CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 1
EXPORTI NG
#1 = gv_l i nno
#2 = gv_col no.

SET PROPERTY OF gs_cel l 1 ' Val ue' = sy- i ndex .

CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 1
EXPORTI NG
#1 = gv_l i ne_cnt r
#2 = gv_col no.

SET PROPERTY OF gs_cel l 1 ' Val ue' = gv_val ue .

gv_col no = gv_col no + 1 .

ENDDO .

Step 7 Set source data area for the chart
Code Part C.7 Setting source data area for the chart
*- - Sour ce dat a ar ea
gv_col no = gv_col no - 1 .

CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 1
EXPORTI NG
#1 = gv_l i nno
#2 = 1.
CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 2
EXPORTI NG
#1 = gv_l i ne_cnt r
#2 = gv_col no.

CALL METHOD OF gs_excel ' Range' = gs_cel l s
EXPORTI NG
#1 = gs_cel l 1
#2 = gs_cel l 2.
CALL METHOD OF gs_cel l s ' Sel ect ' .

Step8 Draw the chart
Code Part C.8 Draw the chart
GET PROPERTY OF gs_appl i cat i on ' Char t s' = gs_char t s .
CALL METHOD OF gs_char t s ' Add' = gs_char t .
CALL METHOD OF gs_char t ' Act i vat e' .
SET PROPERTY OF gs_char t ' Char t Type' = ' 51' . " Ver t i cal bar gr aph
CALL METHOD OF gs_char t ' Set Sour ceDat a'
EXPORTI NG
#1 = gs_cel l s
#2 = 1.

SET PROPERTY OF gs_char t ' HasTi t l e' = 1 .
GET PROPERTY OF gs_char t ' Char t Ti t l e' = gs_char t t i t l e .
GET PROPERTY OF gs_char t t i t l e ' Char act er s' = gs_char t t i t l echar .
SET PROPERTY OF gs_char t t i t l echar ' Text ' = ' Sampl e Gr aph' .


13
An Easy Reference for OLE Automation

Step 9 Locate the chart onto the current worksheet
Code Part C.9 Locating the chart onto the current worksheet
*- - Locat e t he char t ont o t he cur r ent wor ksheet
*- - Act i vat e cur r ent sheet
CALL METHOD OF gs_excel ' Wor kSheet s' = gs_act i vesheet
EXPORTI NG
#1 = gv_sheet _name.
CALL METHOD OF gs_act i vesheet ' Act i vat e' .
CALL METHOD OF gs_char t ' Locat i on'
EXPORTI NG
#1 = 2
#2 = gv_sheet _name.



Step 10 Reposition the chart to a proper place and finish the do-loop
Code Part C.10 Repositioning the chart to a proper place and end of the do-loop counting sheet number
*- - Reposi t i on t he char t on t he wor ksheet ( cut &past e)
CALL METHOD OF gs_act i vesheet ' Char t Obj ect s' = gs_char t obj ect s .
CALL METHOD OF gs_char t obj ect s ' Sel ect ' .
CALL METHOD OF gs_char t obj ect s ' Cut ' .

*- - Sel ect new ar ea
gv_l i ne_cnt r = gv_l i ne_cnt r + 2 .
CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 1
EXPORTI NG
#1 = gv_l i ne_cnt r
#2 = 1.
CALL METHOD OF gs_excel ' Cel l s' = gs_cel l 2
EXPORTI NG
#1 = gv_l i ne_cnt r
#2 = 1.

CALL METHOD OF gs_excel ' Range' = gs_cel l s
EXPORTI NG
#1 = gs_cel l 1
#2 = gs_cel l 2.
CALL METHOD OF gs_cel l s ' Sel ect ' .
CALL METHOD OF gs_act i vesheet ' Past e' .

ENDDO .



Step 11 Free OLE objects to deallocate memory
Code Part C.11 Deallocating the memory
*- - Deal l ocat i ng memor y
FREE: gs_excel , gs_wbookl i st , gs_appl i cat i on, gs_wbook,
gs_act i vesheet , gs_sheet s, gs_newsheet , gs_cel l 1,
gs_cel l 2, gs_cel l s, gs_r ange, gs_f ont , gs_i nt er i or ,
gs_col umns, gs_char t s, gs_char t , gs_char t t i t l e,
gs_char t t i t l echar , gs_char t obj ect s .







14
An Easy Reference for OLE Automation


Result:

The result of the above program will be a number of worksheets having a title area, some
generated data and a chart related to those data.











15
An Easy Reference for OLE Automation


D Conclusion
Now, it is possible to write programs providing integration with MS Office applications
through simple OLE automation techniques. The way how to interpret macro codes to ABAP is
explained so that one can find and implement required functionality using means of the automated
application. As stated before, for requirements that can be fulfilled by DOI, prefer using it. Rainer
Ehre defines SAP Desktop Office Integration (SAP DOI) as a technology that allows
programmers to integrate desktop applications without needing extensive knowledge of the
applications macro language, or even having to be acquainted with the application to any greater
depth than the average end-user. For more information on SAP DOI, you can search for Ehres
published documents. You can also inspect demo programs for SAP DOI at the development class
SOFFICEINTEGRATION. In an R/3 system (after Release 4.0), you can reach related help
documents following: Help R/3 Library, BC Basis Component Integration BC
Desktop Office Integration.
Another component to be mentioned here is SAP BDS (Business Document Service) which
provides utilities for important document services and has its own user interface: BDN Business
Document Navigator.
Consequently, it is highly recommended to make use of SAP DOI for office integration.
For further questions on OLE automation you can refer to the SAP Developer Network
ABAP Programming Forum at http://www.sdn.sap.com .





Author Bio

Serdar imekler is an SAP application developer working for
Havelsan Inc., Turkey. He has experience with ABAP program
development. He is also studying for an M.A. degree at the department of
philosophy at METU. ssimsekler@yahoo.com






16

Das könnte Ihnen auch gefallen