Skip to Content
Technical Articles

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.

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