# InvokeCode Example

**URL:** <https://forum.uipath.com/t/invokecode-example/9311>\
**Category:** Help\
**Tags:** system, activities\
**Created:** [August 7, 2017, 12:28pm UTC](https://forum.uipath.com/t/invokecode-example/9311 "2017-08-07T12:28:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![abskulkarni](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@abskulkarni](https://forum.uipath.com/u/abskulkarni)\
**Post date:** [August 7, 2017, 12:28pm UTC](https://forum.uipath.com/t/invokecode-example/9311/1 "2017-08-07T12:28:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UiJules](https://avatars.discourse-cdn.com/v4/letter/u/3e96dc/32.png) [@UiJules](https://forum.uipath.com/u/UiJules)\
**Post date:** [August 7, 2017, 3:32pm UTC](https://forum.uipath.com/t/invokecode-example/9311/2 "2017-08-07T15:32:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sfranzen](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@sfranzen](https://forum.uipath.com/u/sfranzen)\
**Post date:** [August 7, 2017, 10:48pm UTC](https://forum.uipath.com/t/invokecode-example/9311/3 "2017-08-07T22:48:24Z")

</div>

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](https://global.discourse-cdn.com/uipath/original/2X/3/32df80ad7b073c909cd0a6b9fddbe663927dfc56.png)

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](https://global.discourse-cdn.com/uipath/original/2X/e/e796c7725fa18155bbaf0618de17534a7b45c7ea.png)

Shuffling the alphabet:

 ![workflow](https://global.discourse-cdn.com/uipath/original/2X/e/eb9b01af486f81d57ed6a031e3bce042e790e215.png)

Might give you `ubcaojvhzixtfldywqpnekrsgm`, or `fawuyorqsxjkpiglbhvednmztc` or indeed `hnrjkaiysdtblxupfgzcovwqme`.

---

<div class="post-metadata">

**Author:** ![lasithdilshan20](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/lasithdilshan20/32/9579_2.png) [@lasithdilshan20](https://forum.uipath.com/u/lasithdilshan20)\
**Post date:** [December 21, 2017, 8:10am UTC](https://forum.uipath.com/t/invokecode-example/9311/4 "2017-12-21T08:10:22Z")

</div>

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 . ☹

---

<div class="post-metadata">

**Author:** ![lasithdilshan20](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/lasithdilshan20/32/9579_2.png) [@lasithdilshan20](https://forum.uipath.com/u/lasithdilshan20)\
**Post date:** [December 21, 2017, 8:11am UTC](https://forum.uipath.com/t/invokecode-example/9311/5 "2017-12-21T08:11:46Z")

</div>

@path @prassin6 @vvaidya

---

<div class="post-metadata">

**Author:** ![vvaidya](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@vvaidya](https://forum.uipath.com/u/vvaidya)\
**Post date:** [December 21, 2017, 2:31pm UTC](https://forum.uipath.com/t/invokecode-example/9311/6 "2017-12-21T14:31:28Z")

</div>

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)

```

---

<div class="post-metadata">

**Author:** ![lasithdilshan20](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/lasithdilshan20/32/9579_2.png) [@lasithdilshan20](https://forum.uipath.com/u/lasithdilshan20)\
**Post date:** [December 22, 2017, 8:17am UTC](https://forum.uipath.com/t/invokecode-example/9311/7 "2017-12-22T08:17:37Z")

</div>

@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

```

 ![image](https://global.discourse-cdn.com/uipath/original/2X/0/0f0dcbda02f6621db042a753e8168a67b5367ba0.png)

---

<div class="post-metadata">

**Author:** ![lasithdilshan20](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/lasithdilshan20/32/9579_2.png) [@lasithdilshan20](https://forum.uipath.com/u/lasithdilshan20)\
**Post date:** [December 22, 2017, 8:21am UTC](https://forum.uipath.com/t/invokecode-example/9311/8 "2017-12-22T08:21:04Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![vvaidya](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@vvaidya](https://forum.uipath.com/u/vvaidya)\
**Post date:** [December 22, 2017, 12:04pm UTC](https://forum.uipath.com/t/invokecode-example/9311/9 "2017-12-22T12:04:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lasithdilshan20](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/lasithdilshan20/32/9579_2.png) [@lasithdilshan20](https://forum.uipath.com/u/lasithdilshan20)\
**Post date:** [December 29, 2017, 7:56am UTC](https://forum.uipath.com/t/invokecode-example/9311/10 "2017-12-29T07:56:46Z")

</div>

It’s working thanks a lot

---

<div class="post-metadata">

**Author:** ![lasithdilshan20](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/lasithdilshan20/32/9579_2.png) [@lasithdilshan20](https://forum.uipath.com/u/lasithdilshan20)\
**Post date:** [January 11, 2018, 7:19am UTC](https://forum.uipath.com/t/invokecode-example/9311/11 "2018-01-11T07:19:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sfranzen](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@sfranzen](https://forum.uipath.com/u/sfranzen)\
**Post date:** [January 16, 2018, 3:31pm UTC](https://forum.uipath.com/t/invokecode-example/9311/12 "2018-01-16T15:31:54Z")

</div>

Hi,

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

> [@Unable to create Excel Application Instance in Invoke Code activity](https://forum.uipath.com/t/unable-to-create-excel-application-instance-in-invoke-code-activity/17314):
>
> Scenario: Won’t able to create Excel Application Object in Invoke code. Though I use Microsoft.Office.Interop.Excel Namespace. Steps to reproduce: Current Behavior: error BC31539: Cannot find the interop type that matches the embedded type ‘Microsoft.Office.Interop.ExceI.AppIication’. Are you missing an assembly reference? At line O Exception Type: Argumentaception System.ArgumentException: Error compiling code error BC31539: Cannot find the interop type that matches the embedded type Ex…

---

<div class="post-metadata">

**Author:** ![vvaidya](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@vvaidya](https://forum.uipath.com/u/vvaidya)\
**Post date:** [January 16, 2018, 3:35pm UTC](https://forum.uipath.com/t/invokecode-example/9311/13 "2018-01-16T15:35:21Z")

</div>

Please try this

> [@Interop.Excel.Application is not defined](https://forum.uipath.com/t/interop-excel-application-is-not-defined/16716/4):
>
> Do you have the below assembly in your xaml, if not add it manually and restart your project and try again. [image]

---

<div class="post-metadata">

**Author:** ![1vivek12](https://avatars.discourse-cdn.com/v4/letter/1/ea5d25/32.png) [@1vivek12](https://forum.uipath.com/u/1vivek12)\
**Post date:** [March 2, 2018, 4:40pm UTC](https://forum.uipath.com/t/invokecode-example/9311/14 "2018-03-02T16:40:30Z")

</div>

Hi why am i getting module member declaration expected?

![image](https://global.discourse-cdn.com/uipath/original/2X/4/476349d33f9c5fd543d30adfb383d2f36b27bb3b.png)

---

<div class="post-metadata">

**Author:** ![1vivek12](https://avatars.discourse-cdn.com/v4/letter/1/ea5d25/32.png) [@1vivek12](https://forum.uipath.com/u/1vivek12)\
**Post date:** [March 2, 2018, 4:53pm UTC](https://forum.uipath.com/t/invokecode-example/9311/15 "2018-03-02T16:53:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![1vivek12](https://avatars.discourse-cdn.com/v4/letter/1/ea5d25/32.png) [@1vivek12](https://forum.uipath.com/u/1vivek12)\
**Post date:** [March 2, 2018, 5:58pm UTC](https://forum.uipath.com/t/invokecode-example/9311/16 "2018-03-02T17:58:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![venkat4u](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/venkat4u/32/51215_2.png) [@venkat4u](https://forum.uipath.com/u/venkat4u)\
**Post date:** [April 10, 2018, 9:39am UTC](https://forum.uipath.com/t/invokecode-example/9311/17 "2018-04-10T09:39:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![karthik\_bethi](https://avatars.discourse-cdn.com/v4/letter/k/58956e/32.png) [@karthik\_bethi](https://forum.uipath.com/u/karthik_bethi)\
**Post date:** [May 12, 2018, 7:06pm UTC](https://forum.uipath.com/t/invokecode-example/9311/18 "2018-05-12T19:06:28Z")

</div>

Hi,

Please refer the below tutorial.

[![](https://global.discourse-cdn.com/uipath/original/4X/5/e/4/5e4af5c2000ec23149cd26db7e9295108aee1f7e.jpeg "UiPath Tutorial For Beginner - Invoke Code Activity") ](https://www.youtube.com/watch?v=2v6QBnuN3-c)

Thanks,  
Karthik.

---

<div class="post-metadata">

**Author:** ![blaze.1411](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/blaze.1411/32/62464_2.png) [@blaze.1411](https://forum.uipath.com/u/blaze.1411)\
**Post date:** [July 23, 2018, 6:06am UTC](https://forum.uipath.com/t/invokecode-example/9311/19 "2018-07-23T06:06:13Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![balupad14](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/balupad14/32/462142_2.png) [@balupad14](https://forum.uipath.com/u/balupad14)\
**Post date:** [July 23, 2018, 9:07am UTC](https://forum.uipath.com/t/invokecode-example/9311/20 "2018-07-23T09:07:54Z")

</div>

Hi @blaze.1411,

May be. Because we have to see how we are writing the code. And it accepts [VB.Net](http://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.

> [@Rename a file name using the Invoke Code activity](https://forum.uipath.com/t/rename-a-file-name-using-the-invoke-code-activity/26819/2):
>
> So this does exactly what the move file activity does, but uses InvokeCode instead (which is slower by definition) and relies on VB specific namespace instead of system one. What’s the purpose? Don’t get me wrong - its a nice and short example for InvokeCode, but the application of it makes me a little unease, because someone might actually use that in production.

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

Regards  
Balamurugan.S

[Next page](https://forum.uipath.com/t/invokecode-example/9311.md?page=2)
