Skip to Content

Hello community,

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.

"-Begin-----------------------------------------------------------------
  Report zGetObejct.

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

    "-Variables---------------------------------------------------------
      Data App Type OLE2_OBJECT.
      Data GetApp Type OLE2_OBJECT.

    "-Main--------------------------------------------------------------
      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.
      EndIf.

"-End-------------------------------------------------------------------

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

  "-GetObject-----------------------------------------------------------
    Form GetObject Using ClassName Type String
      Changing Result Type OBJ_RECORD.

      "-Constants-------------------------------------------------------
        Constants CrLf(2) Type c Value %_CR_LF.

      "-Variables-------------------------------------------------------
        Data ScriptCtrl Type OBJ_RECORD.
        Data Expression Type String Value ''.
        Data Module Type String Value ''.

      "-Macros----------------------------------------------------------
        Define AddLine.
          Concatenate Module &1 CrLf Into Module.
        End-Of-Definition.

      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.
        EndIf.
      EndIf.
      Free Object ScriptCtrl.

    EndForm.

"-Subroutines end-------------------------------------------------------
Cheers
Stefan
To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply