Quantcast
Channel: SCN : Document List - ABAP Development
Viewing all articles
Browse latest Browse all 935

How to use VBScript Inside ABAP and Store VBScript Source as Include

$
0
0

Hello community,

 

in different postings I show the possibilities to use different languages with ABAP, e.g. here.

 

Here now an example how to use VBScript inside ABAP and how to store the VBScript source as include.

 

At first create a new include, e.g with the name ZVBSCRIPT001:

 

'-Begin-----------------------------------------------------------------

 

  '-Directives----------------------------------------------------------

    Option Explicit

 

  '-Function plus-------------------------------------------------------

    Function plus(val1, val2)

      plus = val1 + val2

    End Function

 

  '-Function minus------------------------------------------------------

    Function minus(val1, val2)

      minus = val1 - val2

    End Function

 

  '-Function plusminus--------------------------------------------------

    Function plusminus(val1, val2, val3)

      Dim res

      res = plus(val1, val2)

      plusminus = minus(res, val3)

    End Function

 

'-End-------------------------------------------------------------------

 

As you can see, it is a collection of VBScript functions. Don't forget to activate this include. If an error occurs, ignore it.

 

Now create a new function module, e.g. with the name ZREADVBCODE:

 

"-Begin-----------------------------------------------------------------

  Function ZREADVBCODE .


*"--------------------------------------------------------------------

*"*"Lokale Schnittstelle:

*"  IMPORTING

*"     VALUE(I_INCLNAME) TYPE  SOBJ_NAME

*"  EXPORTING

*"     VALUE(E_STRINCL) TYPE  STRING

*"--------------------------------------------------------------------

 

    "-Variables---------------------------------------------------------

      Data resTADIR Type TADIR.

      Data tabIncl Type Table Of String.

      Data lineIncl Type String Value ''.

      Data strIncl Type String Value ''.

 

    "-Main--------------------------------------------------------------

      Select Single * From TADIR Into resTADIR

        Where OBJ_NAME = I_InclName.

      If sy-subrc = 0.

 

        Read Report I_InclName Into tabIncl.

        If sy-subrc = 0.

          Loop At tabIncl Into lineIncl.

            If lineIncl <> ''.

 

              "-Trim leading and trailing spaces------------------------

                Condense lineIncl.

 

              "-If line is no comment-----------------------------------

                If lineIncl+0(1) <> ''''.

                  Concatenate strIncl lineIncl

                    cl_abap_char_utilities=>cr_lf Into strIncl.

                EndIf.

 

              lineIncl = ''.

 

            EndIf.

          EndLoop.

        EndIf.

 

      EndIf.

      E_strIncl = strIncl.

 

  EndFunction.

 

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

 

This function module reads an include and delivers the content of the include as string.

 

As last source the program ZVBSCRIPT:

 

"-Begin-----------------------------------------------------------------

  Report zVBScript.

 

    "-Type pools--------------------------------------------------------

      Type-Pools OLE2.

 

    "-Variables---------------------------------------------------------

      Data ScriptCtrl Type OLE2_OBJECT.

      Data Result Type Integer.

      Data InclCode Type String Value ''.

 

    "-Main--------------------------------------------------------------

      Create Object ScriptCtrl 'MSScriptControl.ScriptControl'.

 

      If sy-subrc = 0 And ScriptCtrl-Handle <> 0 And

        ScriptCtrl-Type = 'OLE2'.

 

        "-Allow to display UI elements----------------------------------

          Set Property Of ScriptCtrl 'AllowUI' = 1.

 

        "-Intialize the VBScript language-------------------------------

          Set Property Of ScriptCtrl 'Language' = 'VBScript'.

 

        "-Read Visual Basic Script code from include file---------------

          Call Function 'ZREADVBCODE'

            Exporting I_InclName = 'ZVBSCRIPT001'

            Importing E_strIncl = InclCode.

 

         "Include ZVBSCRIPT001.

 

        Call Method Of ScriptCtrl 'AddCode' Exporting #1 = InclCode.

 

        If sy-subrc = 0.

 

          Call Method Of ScriptCtrl 'Eval' = Result

            Exporting #1 = 'plusminus(32, 16, 8)'.

 

          Write: / Result. "Result = 40

 

        EndIf.

 

        "-Free the object-----------------------------------------------

          Free Object ScriptCtrl.

 

      EndIf.

 

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

 

It creates an Visaul Basic Script Control and reads the include ZVBSCRIPT001. It adds the source from the include and with the method Eval we execute the VBScript function plusminus.

 

With this way it is very easy to integrate and to use VBScript sources with the SAP development workbench. So it is much more handier as the concatenation of a string with a macro - like in my other examples. This paves the way for an easy polyglot programming.

 

Cheers

Stefan


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>