How to use Late Binding with VBdotNET Invoke Code Activity

A few month ago I presented a solution how to use late binding with C#. And despite the directive Option Strict On in the UiPath Code Activity for VBdotNET, Late Binding can be used with the same approach here as well.

If you try to use e.g. VBScript._AboutBox you get this message:
image

Here an example code how to use Late Binding with VBdotNET Code Activity:

'-UiPath Invoke Code starts here----------------------------------------

'-InvokeMethod----------------------------------------------------------
Dim InvokeMethod As Func(Of Object, String, Object(), Object) = _
Function(obj As Object, methodName As String, methodParams() As Object) As Object
  Return obj.GetType().InvokeMember(methodName, System.Reflection.BindingFlags.InvokeMethod, Nothing, obj, methodParams)
End Function

'-GetProperty-----------------------------------------------------------
Dim GetProperty As Func(Of Object, String, Object(), Object) = _
Function(obj As Object, propertyName As String, propertyParams() As Object) As Object
  Return obj.GetType().InvokeMember(propertyName, System.Reflection.BindingFlags.GetProperty, Nothing, obj, propertyParams)
End Function

'-SetProperty-----------------------------------------------------------
Dim SetProperty As Func(Of Object, String, Object(), Object) = _
Function(obj As Object, propertyName As String, propertyParams() As Object) As Object
  Return obj.GetType().InvokeMember(propertyName, System.Reflection.BindingFlags.SetProperty, Nothing, obj, propertyParams)
End Function

'-Main------------------------------------------------------------------
'-
'- Hint: This example works only in Windows - Legacy compatibility
'-       mode, because MSScriptControl.ScriptControl is only as x86
'-       component available.
'-
'-----------------------------------------------------------------------
Dim VBScript As Object = Nothing
Dim command As String = String.Empty

Try

  VBScript = CreateObject("MSScriptControl.ScriptControl")

  InvokeMethod(VBScript, "_AboutBox", {})

  SetProperty(VBScript, "Language", {"VBScript"})
  SetProperty(VBScript, "AllowUI", {1})

  ' MsgBox "Dies ist ein Test", vbOkOnly, "MessageBox"
  command = "MsgBox ""Dies ist ein Test"", vbOkOnly, ""MessageBox"""
  InvokeMethod(VBScript, "ExecuteStatement", {command})

  VBScript = Nothing

Catch ex As Exception
  MsgBox(ex.Message)
End Try

'-UiPath Invoke Code ends here------------------------------------------

This approach bases on the use of lambda expressions.

Although it may seem a bit contradictory to use this approach, we have the possibility to use Late Binding with VBdotNET in UiPath seamlessly.

2 Likes

Hello Stefan,
thank you for your contribution. We are in the process of migrating from Windows Legacy to Windows and are having some issues. We have many scripts that contain some intelligence. How would calling a VBScript or VBA script work under the Windows framework now? You write that the code would only run in the legacy framework. Thank you for an answer.
Kind regards, Andreas