InvokeCode Example

Hi
Can you direct me to a video tutorial / code example for InvokeCode activity?

Hi,
You can find useful information (as a video tutorial/exercise) in UiPath Academy / Foundation training / Lesson 13 - Projext Organisation - Recap

Hi,

The official training materials might not be up to date yet as this activity has only just been added to the Community edition. I can give a small example right here though. Just as with “Invoke Workflow”, you can specify arguments that will be accessible inside:

arguments

But instead of a workflow filename you enter Visual Basic code you want to execute, directly into an editor window. Suppose I want my code activity to randomly shuffle its string argument. I could program this as follows, which is a version of the Fisher-Yates algorithm:

code

Shuffling the alphabet:

Might give you ubcaojvhzixtfldywqpnekrsgm, or fawuyorqsxjkpiglbhvednmztc or indeed hnrjkaiysdtblxupfgzcovwqme.

Hi,
I need to execute following VB script to move mouse cursor.

Private Sub MoveCursor()
   ' Set the Current cursor, move the cursor's Position,
   ' and set its clipping rectangle to the form. 

   Me.Cursor = New Cursor(Cursor.Current.Handle)
   Cursor.Position = New Point(Cursor.Position.X - 50, Cursor.Position.Y - 50)
   Cursor.Clip = New Rectangle(Me.Location, Me.Size)
End Sub

Please some one guide me to execute this code . :frowning:

@path @prassin6 @vvaidya

I think the above code works only for winforms. Try below in code activity

Arguments:
pt - System.Drawing.Point

pt= New Point(Cursor.Position.X - 50, Cursor.Position.Y - 50)
Cursor.Position = pt

For size part, something like this should work

size = New System.Drawing.Size(10, 10)
pt= New System.Drawing.Point(Cursor.Position.X + 50, Cursor.Position.Y + 50)
Cursor.Position = pt
Cursor.Clip = New Rectangle(pt,size)

@vvaidya
It is not working an error occurred.

 Invoke code : Error compiling code
error BC30451: 'Cursor' is not declared. It may be inaccessible due to its protection level. At line 1
error BC30451: 'Cursor' is not declared. It may be inaccessible due to its protection level. At line 1
error BC30451: 'Cursor' is not declared. It may be inaccessible due to its protection level. At line 2

Main has thrown an exception

Source: Invoke code

Message: Error compiling code
error BC30451: ‘Cursor’ is not declared. It may be inaccessible due to its protection level. At line 1
error BC30451: ‘Cursor’ is not declared. It may be inaccessible due to its protection level. At line 1
error BC30451: ‘Cursor’ is not declared. It may be inaccessible due to its protection level. At line 2

Exception Type: ArgumentException

System.ArgumentException: Error compiling code
error BC30451: ‘Cursor’ is not declared. It may be inaccessible due to its protection level. At line 1
error BC30451: ‘Cursor’ is not declared. It may be inaccessible due to its protection level. At line 1
error BC30451: ‘Cursor’ is not declared. It may be inaccessible due to its protection level. At line 2

at UiPath.Core.Activities.Workflow.CompilerRunner.Compile(String code, Int32 errLineOffset)
at UiPath.Core.Activities.InvokeCode.GetCompilerRunner(String userCode, List`1 args, String imps)
at UiPath.Core.Activities.InvokeCode.Execute(CodeActivityContext context)
at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.ActivityInstance.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

Create Cursor argument of Type System.Windows.Forms.Cursor

It’s working thanks a lot

Hi I need to Execute following Code, Please guide me

Dim oWB As Excel.Workbook
        Dim oSheet As Excel.Worksheet
        Dim oRng As Excel.Range

        ' Start Excel and get Application object.
        oXL = CreateObject("Excel.Application")
        oXL.Visible = True

        Sheets(Sheets.Count).Activate

I need to select last sheet of the excel workbook

Hi,

I tried this but am unable to get it to work. The error I get are the same as this:

Please try this

Hi why am i getting module member declaration expected?

image

Hi ,
I have written this code how can i make it work using invoke code activity please help

Imports System.IO

Module Module1

Private index1 As Integer = New StringHelper().GetIndexofSearchText("C:\Users\vivekver\Documents\UiPath\Nokia Automation\Configuration Manager Nokia 2nd\TEST.txt", "MRBTS-486203")
Private index2 As Integer = New StringHelper().GetIndexofSearchText("C:\Users\vivekver\Documents\UiPath\Nokia Automation\Configuration Manager Nokia 2nd\TEST.txt", "[TopologyNode: PLMN-PLMN/MRBTS-486215]")
Public index_Hotkey As Integer = New NoOfHotkeys().GetHotkey(index1, index2)

Public Class NoOfHotkeys
    Public Function GetHotkey(ByVal index1 As Int32, ByVal index2 As Int32)
        Dim index_Hotkey As Integer = index2 - index1
        Return index_Hotkey

    End Function

End Class
Public Class StringHelper

    Public Function GetIndexofSearchText(ByVal Filename As String, ByVal SearchText As String) As Integer
        Dim index As Integer = 0
        Dim lines As List(Of String) = File.ReadLines(Filename).ToList()
        For Each line In lines
            If line.Contains(SearchText) Then
                Return index
            End If

            index += 1
        Next

        Return index


    End Function
End Class

Sub Main()


End Sub

End Module

i want index_Hotkey as output to my uipath workflow.Please help

Hi the activity is to read text file line by line until it finds a string and simuntaneously increment a index to know row index of line till it finds the string.Please help

Hi,

i am new to uipath and i am trying to use the invoke code activity to work on excel for that i have imported Microsoft.office.interop.excell through import tab in uipath studio but still gettting error for Excel.

could you pleae guide me how to use the external dll in invoke code.

Hi,

Please refer the below tutorial.

Thanks,
Karthik.

Does Invoke code reduces the performance of the Robot process compared to Activities ??

I need with a justifying answer. kindly help me out.

Hi @blaze.1411,

May be. Because we have to see how we are writing the code. And it accepts VB.Net.
The activities has been handled object well. I am not sure that we can able to do all the things in the Invoke code.
I read the invoke code will get slow . I have seen a post the below.

Note :slight_smile:
Some situation we can not avoid the Invoke code activity …

Regards
Balamurugan.S