Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ask Excel to execute C# code

Tags:

c#

excel

vb.net

dll

Is it possible to ask Excel to start a C# method?
How would you implement such a call in Excel?

(i.e. instead of programming in VB, I would like to program in C#)

I can imagine using a VB-macro to start a C# application in the background but maybe you know a nicer way?

For example, the C#-code shall be executed upon a click in a particular Excel cell.

like image 577
iKK Avatar asked Nov 19 '25 06:11

iKK


1 Answers

Well you could open a program via VBA. This VBA script gets called by clicking on the Excel-Cell:

var Path = "MYPROGRAMPATH"
var Argument = "MYARGUMENT"
x = Shell("""" & Path & """ """ & Argument & """", vbNormalFocus) 

To react on a cell change, use the following event:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'YOUR CODE
End Sub

Then program your C# application and let it determine the arguments.
Your program should react according to the filtered arguments.

This can be done with the Environment.GetCommandLineArgs-Method.

public static void Main() 
{
   Console.WriteLine();
   //  Invoke this sample with an arbitrary set of command line arguments.
   String[] arguments = Environment.GetCommandLineArgs();
   Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments));
   //Handling of arguments here, switch-case, if-else, ... 
}
like image 117
jAC Avatar answered Nov 21 '25 20:11

jAC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!