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.
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.
The array is created in VBScript function and as return parameter transfered to JScript. With toArray it is converted to a JScript array.
The array is created in VBScript function and as return parameter transfered to JScript. In JScript it is necessary to use VBArray method.
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.
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
Hello Venkatesh Guttikonda,
I added an example above.
Best regards
Stefan
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
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