0

I am trying to write something that would access methods I have written in a WPF c# application, from a vba worksheet, an intermediary would be acceptable, as well as any command line solution, as long as I can run this intermediary from excel where the data I need to send will be kept.

Does anyone know any decent solutions for sending data to a c# without registering the dll, and preferably in a portable way that I can move to other machines easily?

Any help in that matter would be greatly appreciated, I am looking for pointers in the right direction as a COM solution is not viable right now.

Cheers.

Community
  • 1
  • 1
  • 1
    [Using a C# dll inside EXCEL VBA](http://stackoverflow.com/q/6340041/2140173) and [Call unregistered .net DLL from Excel](http://stackoverflow.com/q/5933352/2140173) –  Jul 22 '15 at 11:03

1 Answers1

1

You could write a C# Console Application which accepts command-line arguments, then call this using the Shell command.

Here's a sample Console application which handles the input parameters: using System;

namespace ConsoleAppWithParams
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Number of command line parameters = {0}",
               args.Length);
            foreach (string s in args)
            {
                Console.WriteLine(s);
            }
            Console.ReadLine();
        }
    }
}

Note that the parameters are coming from the command-line, so they are strings. You'd need to validate and convert the parameters if you need other data types.

You can call this from Excel VBA as follows:

Sub UseCSharpApp()
    Shell "D:\Documents\ConsoleAppWithParams.exe Tom Dick Harry"
End Sub

If your C# application is fairly straightforward, then the only requirement would be for it to be copied to the expected directory, and for the correct version (or a more recent version) of the .NET Framework to be installed on the PC. If the C# app is written to target .NET Framework 2.0, then it's likely that most Windows PCs in a business environment would be able to use this, but obviously you should check whether that's the case in your environment.

Note that with this approach, you're limited to passing in values when first calling the application. This wouldn't be a useful solution in all scenarios, but might work if that's all you need to do.

Jane
  • 851
  • 4
  • 9