Skip to Content

Manipulating MS Office documents from ABAP (OLE2)

The idea to start using the OLE2 interface came from the repetitive steps that needed to be done to fill out a technical design (TD) template. For instance for a new ABAP program a lot of information for the TD can be directly passed from the SAP system, such as program name, description, author, date, version number, etc. When you have to make changes to this program you could also use the OLE2 interface to open the existing TD and automatically add a new line to the version history as well as inserting a new paragraph which describes the changes for this version. The developer then only needs to enter the actual text describing the changes.

Desktop Office Integration framework

At the time of writing I was not aware of the Desktop Office Integration framework which provides an object oriented framework on top of this OLE2 interface. Since I did not dive into it yet I cannot comment on it but I intent to blog about it once I do. More information can be found in the online help. Thanks to the kind moderator for making me aware of this.

The following code example will start MS Word, open a blank document, write ‘Olé World!’ in it and save it to your local harddrive:



REPORT  zdemo_ole_interface.


DATA: v_objword      TYPE ole2_object,
      v_documents    TYPE ole2_object,
      v_document     TYPE ole2_object,
      v_objselection TYPE ole2_object.

SET PROPERTY OF v_objword ‘Visible’ = ‘1’ .
CALL METHOD OF v_objword ‘Documents’ = v_documents.

CALL METHOD OF v_documents ‘Add’ = v_document.

CALL METHOD OF v_objword ‘Selection’ = v_objselection.

CALL METHOD OF v_objselection ‘TypeText’
  EXPORTING #1 = ‘Olé World!’.

CALL METHOD OF v_document ‘SaveAs’
  #1 = ‘C:\testole.doc’.


The property ‘Visible’ in this case is ‘1’ which means you will actually see the steps being performed (Word being started, new document being opened, text being written). If it is ‘0’ the steps are done in the background (which should of course be faster). In that case you will not see MS Word being opened but the file will still be saved to your harddrive.

Object Browser

The steps that you can perform are limited to the macro-functionalities of the MS Office applications, in this case MS Word. You can for instance use the Find-functionality to position your cursor in the document where you want to create an additional paragraph. After this you can update the Table of Contents. An overview of the available functions can be found in the macro-editor of the MS Office application. For MS Word 2007 this can be found under the View-tab. If you choose View Macro and press Create you will go to the Visual Basic macro-editor. By pressing F2 you will get the Object Browser:

Visual Basic - Object browser

If we scroll down to object Documents you can see the interface of the function, its properties, methods and the returning object:

Object Documents

You see that methods have the green ‘flying object’ icon and properties have the ‘pointing finger’ icon. In our example we used the following line to call method Add of object Documents:

CALL METHOD OF v_documents ‘Add’ = v_document.

v_document is the object that’s returned from the Add function and this is the instance of the newly created document. Parameters Template, NewTemplate, DocumentType and Visible are optional (bracketed) and are omitted in our coding. If we wanted to pass parameter Visible we should have added EXPORTING #4 = ‘0’ (for making the document invisible) because Visible is the fourth parameter.

If we want to get the number of opened documents we should read the Count property. This is done by the following statement:

Data: lv_counter TYPE i.

GET PROPERTY OF v_documents ‘Count’ = lv_counter.


You can think of some useful applications for this OLE2 interface (also using Excel, Visio, etc.). You can for instance fill out an MS Word template with SAP data. After the template is opened and filled you can make some manual adjustments and save it to your local drive, BDS or attach it in a mail, etc.) Filling the form fields can be done using the find and replace functionality. You search for the name of the variable and you replace it with the value you have read in your ABAP program. You can even fill a Word document with values read from a smartform (or better: the XML data from a smartform). I’m not saying this is the best form-solution, but if users need to be able to edit an MS Office document before saving it, this could be a workable solution.

I am no guru on the subject but I did like to play around with OLE2. I am interested to hear about other applications and experiences with it.

You must be Logged on to comment or reply to a post.
  • …and a nice one for that. I didn’t know the Desktop Office Int. Framework either, so thanks for pointing that out too. Keep up the good work!

    Cheers, Fred

  • According to me, when you want to use OLE2 in ABAP programs the basis for this work is the macro recording available in MS office applications, which generates VBA (visual basic for applications) code. Then you (manually) convert it to OLE via very simple rules.
    I never saw explanations which explain that (or ABAP snippet which does it for you :D)), I would find it very useful and time-saver. If someone is interested to do it…
    • Macro recordings are indeed a good starting point for the ABAP-code you are about to write. If you record a macro for the example I used you’ll see something like this in your recording:

      Selection.TypeText Text:=”Olé World!”

      You can lookup Selection in the Object Browser. TypeText is one of the available methods and Text is the only parameter for that function. For functions with more parameters you’ll need to take a look at the interface of the method to see how to pass values to the right parameters (#1, #2, etc.).

      The Desktop Office Integration framework provides a more intuitive layer to call these functions (for instance the Replace function in I_OI_WORD_PROCESSOR_DOCUMENT) so you do not need to look up the actual interface in the macro editor. Of course translating from a macro recording will be a bit harder because it’s not a direct translation of VB to ABAP.

    • Sometimes it is very poor. When I am working on a system through VPN, I can see my Excel fill one field after the other for a minute or two (to get like 2, 3 pages of data).
      But the users (who don´t use the VPN) can get the document ready very quickly.
      You can test the performance for your self. It is very easy to write a simple OLE program to export any data you want into Excel for example.
      If you´re worried about the performance or you would like to work with office documents on the background, you can try my alternative approach using open XML format for Office docs. You can find the link below.