Invoke code transformation problems

activities
invoke_code

#1

Hi everyone!

I have a problem with Invoke code activity because I have a Recursive code in VB in an Excel file and I would like to include it in a workflow.

The code is: (Note: It is not my code, I found it on Internet but runs perfectly as a formula in Excel file)

Public Function Similarity(ByVal String1 As String, _
ByVal String2 As String, _
Optional ByRef RetMatch As String, _
Optional min_match = 1) As Single
Dim b1() As Byte, b2() As Byte
Dim lngLen1 As Long, lngLen2 As Long
Dim lngResult As Long

If UCase(String1) = UCase(String2) Then
Similarity = 1
Else:
lngLen1 = Len(String1)
lngLen2 = Len(String2)
If (lngLen1 = 0) Or (lngLen2 = 0) Then
Similarity = 0
Else:
b1() = StrConv(UCase(String1), vbFromUnicode)
b2() = StrConv(UCase(String2), vbFromUnicode)
lngResult = Similarity_sub(0, lngLen1 - 1, _
0, lngLen2 - 1, _
b1, b2, _
String1, _
RetMatch, _
min_match)
Erase b1
Erase b2
If lngLen1 >= lngLen2 Then
Similarity = lngResult / lngLen1
Else
Similarity = lngResult / lngLen2
End If
End If
End If

End Function

Private Function Similarity_sub(ByVal start1 As Long, ByVal end1 As Long, _
ByVal start2 As Long, ByVal end2 As Long, _
ByRef b1() As Byte, ByRef b2() As Byte, _
ByVal FirstString As String, _
ByRef RetMatch As String, _
ByVal min_match As Long, _
Optional recur_level As Integer = 0) As Long
’* CALLED BY: Similarity *(RECURSIVE)

Dim lngCurr1 As Long, lngCurr2 As Long
Dim lngMatchAt1 As Long, lngMatchAt2 As Long
Dim I As Long
Dim lngLongestMatch As Long, lngLocalLongestMatch As Long
Dim strRetMatch1 As String, strRetMatch2 As String

If (start1 > end1) Or (start1 < 0) Or (end1 - start1 + 1 < min_match) _
Or (start2 > end2) Or (start2 < 0) Or (end2 - start2 + 1 < min_match) Then
Exit Function '(exit if start/end is out of string, or length is too short)
End If

For lngCurr1 = start1 To end1
For lngCurr2 = start2 To end2
I = 0
Do Until b1(lngCurr1 + I) <> b2(lngCurr2 + I)
I = I + 1
If I > lngLongestMatch Then
lngMatchAt1 = lngCurr1
lngMatchAt2 = lngCurr2
lngLongestMatch = I
End If
If (lngCurr1 + I) > end1 Or (lngCurr2 + I) > end2 Then Exit Do
Loop
Next lngCurr2
Next lngCurr1

If lngLongestMatch < min_match Then Exit Function

lngLocalLongestMatch = lngLongestMatch
RetMatch = β€œβ€

lngLongestMatch = lngLongestMatch _

  • Similarity_sub(start1, lngMatchAt1 - 1, _
    start2, lngMatchAt2 - 1, _
    b1, b2, _
    FirstString, _
    strRetMatch1, _
    min_match, _
    recur_level + 1)
    If strRetMatch1 <> β€œβ€ Then
    RetMatch = RetMatch & strRetMatch1 & β€œ"
    Else
    RetMatch = RetMatch & IIf(recur_level = 0 _
    And lngLocalLongestMatch > 0 _
    And (lngMatchAt1 > 1 Or lngMatchAt2 > 1) _
    , "
    ”, β€œβ€)
    End If

RetMatch = RetMatch & Mid$(FirstString, lngMatchAt1 + 1, lngLocalLongestMatch)

lngLongestMatch = lngLongestMatch _

  • Similarity_sub(lngMatchAt1 + lngLocalLongestMatch, end1, _
    lngMatchAt2 + lngLocalLongestMatch, end2, _
    b1, b2, _
    FirstString, _
    strRetMatch2, _
    min_match, _
    recur_level + 1)

If strRetMatch2 <> β€œβ€ Then
RetMatch = RetMatch & β€œ" & strRetMatch2
Else
RetMatch = RetMatch & IIf(recur_level = 0 _
And lngLocalLongestMatch > 0 _
And ((lngMatchAt1 + lngLocalLongestMatch < end1) _
Or (lngMatchAt2 + lngLocalLongestMatch < end2)) _
, "
”, β€œβ€)
End If

Similarity_sub = lngLongestMatch

End Function

I know that I should make some transformations to put this code in a Invoke code activity, but it is impossible to me to finish the transformation and save the code.

Could anyone help me?

Thank you in advance.


#2

I think it’s not possible to create functions in Invoke Code activity. You can only add parts that could be included in single procedure. You can treat invoke code as interior of procedure.

Pure vba will not work, you should use VB .Net syntax. Remember that β€˜Strict on’ option is enabled in UiPath - no late binding is allowed.


#3

And how could I try to invoke code as interior of procedure?


#4

Example Invoke code activity.

Set arguments:

Create code:
image

You can use single commands, if statements, loops etc. but you can’t define your own function. Recursive code will not work here.


#5

Hi!

I tried to transform in procedure but I have some error.

The argurments are 2 strings and a double as a result.

The code is the next one (I repeat that it is not my code)

Dim b1() As Byte, b2() As Byte
Dim lngLen1 As Double, lngLen2 As Long
Dim lngResult As Long

If UCase(String1) = UCase(String2) Then
resultado = 1
Else:
lngLen1 = Len(String1)
lngLen2 = Len(String2)
If (lngLen1 = 0) Or (lngLen2 = 0) Then
resultado = 0
Else:
b1() = StrConv(UCase(String1), vbFromUnicode)
b2() = StrConv(UCase(String2), vbFromUnicode)
lngResult = Similarity_sub (0,convert.ToInt64(lngLen1 - 1), _
0, lngLen2 - 1, _
b1, b2, _
String1, _
RetMatch, _
min_match)
Erase b1
Erase b2
If lngLen1 >= lngLen2 Then
resultado = lngResult / lngLen1
Else
resultado = lngResult / lngLen2
End If
End If
End If

End Sub

Private Function Similarity_sub(ByVal start1 As Long, ByVal end1 As Long, _
ByVal start2 As Long, ByVal end2 As Long, _
ByRef b1() As Byte, ByRef b2() As Byte, _
ByVal FirstString As String, _
ByRef RetMatch As String, _
ByVal min_match As Long, _
Optional recur_level As Double = 0) As Long
’* CALLED BY: Similarity *(RECURSIVE)

Dim lngCurr1 As Long, lngCurr2 As Long
Dim lngMatchAt1 As Long, lngMatchAt2 As Long
Dim I As Long
Dim lngLongestMatch As Long, lngLocalLongestMatch As Long
Dim strRetMatch1 As String, strRetMatch2 As String
Dim longactual1, longactual2, final1, final2 As Double

If (start1 > end1) Or (start1 < 0) Or (end1 - start1 + 1 < min_match) _
Or (start2 > end2) Or (start2 < 0) Or (end2 - start2 + 1 < min_match) Then
Exit Function '(exit if start/end is out of string, or length is too short)
End If

For lngCurr1 = start1 To end1
For lngCurr2 = start2 To end2
I = 0
Do Until b1(convert.ToInt32(lngCurr1 + I)) <> b2(convert.ToInt32(lngCurr2 + I))
I = I + 1
If I > lngLongestMatch Then
lngMatchAt1 = lngCurr1
lngMatchAt2 = lngCurr2
lngLongestMatch = I
End If
If (lngCurr1 + I) > end1 Or (lngCurr2 + I) > end2 Then Exit Do
Loop
Next lngCurr2
Next lngCurr1

If lngLongestMatch < min_match Then Exit Function

lngLocalLongestMatch = lngLongestMatch
RetMatch = β€œβ€

lngLongestMatch = lngLongestMatch _

  • Similarity_sub(start1, lngMatchAt1 - 1, _
    start2, lngMatchAt2 - 1, _
    b1, b2, _
    FirstString, _
    strRetMatch1, _
    min_match, _
    recur_level + 1)
    If (strRetMatch1 <> β€œβ€) Then
    RetMatch = RetMatch + strRetMatch1 + β€œ"
    Else
    RetMatch = String.Concat(retmatch, IIf(recur_level = 0 _
    And lngLocalLongestMatch > 0 _
    And (lngMatchAt1 > 1 Or lngMatchAt2 > 1) _
    , "
    ”, β€œβ€))

End If

RetMatch = String.Concat (RetMatch, Mid$(FirstString, convert.ToInt32(lngMatchAt1 + 1), convert.ToInt32(lngLocalLongestMatch)))

lngLongestMatch = lngLongestMatch _

  • Similarity_sub(lngMatchAt1 + lngLocalLongestMatch, end1, _
    lngMatchAt2 + lngLocalLongestMatch, end2, _
    b1, b2, _
    FirstString, _
    strRetMatch2, _
    min_match, _
    recur_level + 1)

If strRetMatch2 <> β€œβ€ Then
RetMatch = RetMatch + β€œ" + strRetMatch2
Else
retmatch = String.Concat(Retmatch, IIf(recur_level = 0 _
And lngLocalLongestMatch > 0 _
And ((lngMatchAt1 + lngLocalLongestMatch < end1) _
Or (lngMatchAt2 + lngLocalLongestMatch < end2)) _
, "
”, β€œβ€))
End If

Similarity_sub = lngLongestMatch

End Function

When I execute, the system show me this error

I think the problem is the transformation about the code.

Could you help me?

Thanks a lot!


#6

Hi,

The answer is the same. Because the Invoke Code activity is basically a Visual Basic Sub in itself, you can’t declare anything new inside, like additional Subs or Functions. If you want to have custom functions, you will have to resort to defining those in your own library, which can then be packaged, installed in Studio and then added to a project’s imports. The links in this post describe the procedure:

Alternatively you could look into Windows PowerShell scripting, which works well with the related UiPath activities.