3

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.

3
  • 3
    Could you explain what you're doing and hoping to ultimately accomplish? It sounds like you're trying to do something super hack-y. We might be able to suggest a better way if we had some context. Commented Jun 1, 2015 at 13:40
  • Yes, I can explain a bit more in detail what I try to accomplish: Ultimately, a C# application with a nice GUI will do the entire job that eventually needs to be done. This (final) C#-application will have to deal with several Excel-Files. It will open the Excel-Files, do some constraint-Checks on Worksheets of several of these Excel-Files and regroup some of its content according to a needed order. The GUI will assist the user to do the task with the several Excel-Files. So far so good. Commented Jun 1, 2015 at 14:01
  • NOW, I try to split the work of building the GUI-architecture in several sub-tasks, in order to get my small team of programmers get going. One sub-task shall be to test the contraint-checks with a single Excel-File. And I do want to be able to program this task directly in C# in order to simplify the integration of that architectural sub-task in the final Appliation later on ! It is not worth programming the contraint-checks in VBA since this code won't be of any help in the final C# application. Moreover, my team knows better C# than VBA. Commented Jun 1, 2015 at 14:01

2 Answers 2

3

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, ... 
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have to admit, that this is kind of hacky. But it should do the trick. And if it works, who cares ;-)
Thank you Janes Abou Chleih !! I will give it a try !
Could you please elaborate more on the syntax of "x = Shell(Path&Argument, vbNormalFocus)". It did not work for me right away!. If I write "x = Shell(Path, vbNormalFocus)" everything works - however the "&argument" somehow does not. I need some more help on this... Thank you very much !
0

I've found a solution to the VBA macro:

Sub Button1_Click()

    Dim pathStr As String
    Dim argumentStr As String

    pathStr = "C:/Users/.../Desktop/temp/Trial02.exe"
    argumentStr = "Any argument string"

    Call Shell("""" & pathStr & """ """ & argumentStr & """", vbNormalFocus)

End Sub

1 Comment

Oh - I guess we were same speed ;) Thanks !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.