Search code examples
excelvbauipath

How to return object to UiPath in VBA?


I am trying to return an object from function Find123. I have called it from sub procedure Retrieve123.

When I comple it says function not defined and it highlights the CObj method.

Could you tell me a way to convert integer or string into object and return it in VBA?

Sub Retrieve123()
    Call Find123()
End Sub

Function Find123()As Object
    Dim val As String
    val="100"
    Dim obj As Object
    Set obj=CObj(val)
    Set Find123 = obj
End Function

This is how I get the error message enter image description here

When I assign the string value into the function it gives this type mismatch error.
enter image description here

I need to convert the string into object because in UiPath we can only return type object as the Invoke VBA activity's output is object type.


Solution

  • You don't have to convert anything to an object, yet UiPath treats return values from the Invoke VBA activity as objects (since it wouldn't know what is being returned). You can safely return a primitive type from your function - an integer in your case - store it in a object-typed variable in UiPath, and then cast it to integer again.

    Here's a general example. Note that result is of type object while i is an integer. Inside the Assign activity, i is being cast to an integer.

    VB.NET equivalent:

    i = CType(result, Integer)
    

    Type Conversion in UiPath