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

Tip: How To Execute VBScript Seamlessly in IRPA

After my experiment to integrate PowerShell in IRPA, I remembered an approach, which I published years ago, how to use VBScript in ABAP. We can use the same approach in IRPA to use VBScript seamlessly in IRPA. Therefore we use the Microsoft Script Control. It allows the user to execute script code for any scripting engine, e.g. like VBScript, which is included with the Script Control.

The code is very easy to understand. At first it is necessary to create an MSScriptControl object and to set the properties AllowUI, to display UI elements, and the Language, in our case VBScript. To execute VBScript code we have different possibilities:

  • ExecuteStatement = Runs a specified statement
  • Run = Runs a specified procedure
  • Eval = Runs an expression and returns the result

To embed the VBScript code inside JScript I use a tiny function to simulate here-strings in JScript, like in PowerShell.

function hereString(f) {
  return f.toString().
      replace(/^[^\/]+\/\*!?/, '').
      replace(/\*\/[^\/]+$/, '');
}

Here now the code:

// ----------------------------------------------------------------
//   Step: Display_msgbox_Hello
// ----------------------------------------------------------------
GLOBAL.step({ Display_msgbox_Hello: function(ev, sc, st) {
    var rootData = sc.data;
    ctx.workflow('HelloWorkflow', '26c01308-c7d9-4e09-a8cc-ba510c04d410') ;

    var MSScrCtrl = new ActiveXObject("MSScriptControl.ScriptControl");
    MSScrCtrl.AllowUI = 1;
    MSScrCtrl.Language = 'VBScript';
    MSScrCtrl.ExecuteStatement('MsgBox "Hello World from VBScript in IRPA!", VBOkOnly, "ExecuteStatement"');

    var VBSCode = hereString(function() {/*!
      Option Explicit
      Sub foo()
        Dim Text
        Text = "Hello World from VBScript in IRPA!"
        MsgBox Text, VBOkOnly, "Run"
      End Sub
    */});
    MSScrCtrl.AddCode(VBSCode);
    MSScrCtrl.Run('foo');

    MSScrCtrl.Reset();

    VBSCode = hereString(function() {/*!
      Option Explicit
      Function foo(Text)
        foo = MsgBox(Text, VBYesNo, "Eval")
      End Function
    */});
    MSScrCtrl.AddCode(VBSCode);
    var MsgBoxText = 'Hello World from VBScript in IRPA';
    var FunctionCall = 'foo("' + MsgBoxText + '")';
    var res = MSScrCtrl.Eval(FunctionCall);
    //res = 6 if Yes or res = 7 if No
    ctx.log(res);

    MSScrCtrl.Reset();

    // Display msgbox 'HelloWorld2019'
    // Creates the popup according to selected template, sets title and message and displays it.
    var HelloWorld2019 = ctx.popup('HelloWorld2019', e.popup.template.Ok);
    HelloWorld2019.open({ title: 'HelloWorld2019', message: "Hello World"});
    // Wait until the end user closes the popup.
    HelloWorld2019.waitResult(function(res) {
    // End user has closed the popup, continue monitoring.
    sc.endStep(); // end Scenario
    return;
    });
}});

Here the results:

To close the messagebox I press yes, which delivers 6 in the log.

Great, it works as expected.

This approach offers the possibility to use VBScript seamlessly inside IRPA. On this way it is also possible to use existing consolidated scripts furthermore in the IRPA context, without any effort.

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Stefan Schnell
      Stefan Schnell

      How to Transfer Arrays Between JScript and VBScript

      Here, as an addition, an example how to transfer arrays as parameters between JScript and VBScript from MSScriptControl.

      1. Gets string array from VBScript to JScript
        The array is created in VBScript function and as return parameter transfered to JScript. With toArray it is converted to a JScript array.
      2. Gets multidimensional integer Array from VBScript to JScript
        The array is created in VBScript function and as return parameter transfered to JScript. In JScript it is necessary to use VBArray method.
      3. Sets string array from JScript to VBScript
        The array is created in JScript and transfered via AddObject method to VBScript, now it is possible to work with the array in the sub routine.
      4. Takes the string array from JScript, change and get it from VBScript to JSript
      //-Begin----------------------------------------------------------------
      
      //-Sub log--------------------------------------------------------------
      function log(logText) {
        if(typeof WScript === "object") {
          WScript.Echo(logText);
        } else {
          console.log(logText);
        }
      }
      
      //-Function hereString--------------------------------------------------
      function hereString(strHere) {
        return strHere.toString().
          replace(/^[^\/]+\/\*!?/, '').
          replace(/\*\/[^\/]+$/, '');
      }
      
      //-Sub Main-------------------------------------------------------------
      function Main() {
      
        var MSScrCtrl = new ActiveXObject("MSScriptControl.ScriptControl");
        MSScrCtrl.AllowUI = 1;
        MSScrCtrl.Language = "VBScript";
      
      //-Begin VBScript-------------------------------------------------------
        var VBSCode = hereString(function() {/*!
      
      Option Explicit
      
      Function getStrArray()
        getStrArray = Array("Apples", "Bananas", "Oranges")
      End Function
      
      Function getMultiIntArray()
        Dim arr(1, 1)
        arr(0, 0) =  2
        arr(0, 1) =  4
        arr(1, 0) =  8
        arr(1, 1) = 16
        getMultiIntArray = arr
      End Function
      
      Sub setJSArray()
        Dim element
        For Each element In strJSArr
          MsgBox(element)
        Next  
      End Sub
      
      Function getsetStrArray()
        Dim strVBArr, i
        strVBArr = Split(strJSArr, ",")
        For i = 0 To UBound(strVBArr)
          Select Case strVBArr(i)
            Case "Cherries"
              strVBArr(i) = "Beans"
            Case "Grapes"
              strVBArr(i) = "Tomatoes"
            Case "Strawberries"
              strVBArr(i) = "Potatoes"
          End Select
        Next
        getsetStrArray = strVBArr
      End Function
      
        */});
      //-End VBScript---------------------------------------------------------
      
        MSScrCtrl.AddCode(VBSCode);
      
      
        //-Get string array from VBScript to JScript--------------------------
        var strVBArr = MSScrCtrl.Eval('getStrArray()').toArray();
        for(var i = 0; i < strVBArr.length; i++) {
          log(strVBArr[i]);
        }
      
        //-Get multidimensional integer array from VBScript to JScript--------
        var intMultiVBArr = new VBArray(MSScrCtrl.Eval('getMultiIntArray()'));
        for(var i = 1; i <= intMultiVBArr.dimensions(); i++) {
          for(var j = intMultiVBArr.lbound(i); j <= intMultiVBArr.ubound(i); j++) {
            log(intMultiVBArr.getItem(i - 1,j))
          }
        }
      
        //-Set string array from JScript to VBScript--------------------------
        var strJSArr = ["Cherries", "Grapes", "Strawberries"];
        MSScrCtrl.AddObject("strJSArr", strJSArr);
        MSScrCtrl.Run("setJSArray");
      
        //-Change JScript array in VBScript-----------------------------------
        strJSArr = MSScrCtrl.Eval('getsetStrArray()').toArray();
        for(var i = 0; i < strVBArr.length; i++) {
          log(strJSArr[i]);
        }
        
        MSScrCtrl.Reset();
      
      }
      
      //-Main-----------------------------------------------------------------
      Main();
      
      //-End------------------------------------------------------------------
      Author's profile photo Venkatesh Guttikonda
      Venkatesh Guttikonda

      Hi Stefan,

      Thanks for your help. This helps me a lot.

      is it possible to send and receive array of values in one single function call between VBScript and JScript ?

      Regards,

      Venkatesh

      Author's profile photo Stefan Schnell
      Stefan Schnell

      Hello Venkatesh Guttikonda,

      I added an example above.

      Best regards
      Stefan

      Author's profile photo Venkatesh Guttikonda
      Venkatesh Guttikonda

      Many Thanks Stefan.

      As you know, our SAP IRPA still evolving in Win Automation, it would be great if you can write blog post on “How to execute AutoIT script seamlessly in IRPA”.

      Thanks

      Venkat

      Author's profile photo Paul Wang
      Paul Wang

      Hi Stefan Schnell ,

       

      It is a great post, thanks for sharing. While I am facing an issue on this approach.

      What i want to achieve is to right click on a cell of a table, there will be a context menu displayed, select a item on the context menu.

       

      //Key part of VB script as below:

      session.findById("tableid").setCurrentCell ' + rows[i] + ',"ZZSTATUS"
      session.findById("tableid").contextMenu
      session.findById("tableid").selectContextMenuItem "LDETS_STATUS_INACTIVE"

      Actually this code works, but some times (~10%), it failed: the context menu was displayed and hangs there, which blocked bot's execution.

      Do you have any idea?

       

      Best regards,

      Paul