Skip to Content
Technical Articles
Author's profile photo Stefan Schnell

How to simulate GetObject in ABAP

A longer time ago I asked here for the possibility to use GetObject inside ABAP. GetObject returns a reference to an existing object provided by a COM component. ABAP don’t offers any command to get these informations. Here I present a solution to “simulate” GetObject via VBScript. But this solves not only this problem, it shows also how to use VBScript inside ABAP.

  Report zGetObejct.

    "-Type pools--------------------------------------------------------
      Type-Pools OLE2.

      Data App Type OLE2_OBJECT.
      Data GetApp Type OLE2_OBJECT.

      Create Object App 'Excel.Application'.
      If sy-subrc = 0.
        PerForm GetObject Using 'Excel.Application' Changing GetApp.

          "- Now you can use GetApp or App
          "- If an object of Excel already exists, it is not
          "- necessary to call Create Object

        Free Object App.


"-SubRoutines begin-----------------------------------------------------

    Form GetObject Using ClassName Type String
      Changing Result Type OBJ_RECORD.

        Constants CrLf(2) Type c Value %_CR_LF.

        Data ScriptCtrl Type OBJ_RECORD.
        Data Expression Type String Value ''.
        Data Module Type String Value ''.

        Define AddLine.
          Concatenate Module &1 CrLf Into Module.

      AddLine 'Function GetObj(ClassName)'.
      AddLIne '  Dim oObj'.
      AddLine '  Set oObj = GetObject(, ClassName)'.
      AddLine '  If IsObject(oObj) Then'.
      AddLine '    Set GetObj = oObj'.
      AddLine '  End If'.
      AddLine 'End Function'.

      Concatenate 'GetObj("' ClassName '")' Into Expression.
      Create Object ScriptCtrl 'MSScriptControl.ScriptControl'.
      If sy-subrc = 0.
        Set Property Of ScriptCtrl 'AllowUI' = 1.
        Set Property Of ScriptCtrl 'Language' = 'VBScript'.
        Call Method Of ScriptCtrl 'AddCode' Exporting #1 = Module.
        If sy-subrc = 0.
          Call Method Of ScriptCtrl 'Eval' = Result
            Exporting #1 = Expression.
      Free Object ScriptCtrl.


"-Subroutines end-------------------------------------------------------


Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.