Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Stefan-Schnell
Active Contributor
0 Kudos
1,255
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-------------------------------------------------------

Labels in this area