0

I have a large codebase built in C# (classes and functions), compiled to .NET 4.7.2 in VS2019 and need to call to the compiled C# assembly (compiles to .dll) from VBA in excel. What is the path of least resistance to achieve this, considering I need to deploy the final VBA + C# dll (and whatever wrapper layers I may need) to client machines that won't have admin rights.

I can recompile the C#, if necessary. If I cannot call directly to C#, could I go via a C++ native wrapper, and then on to C#? (or do I need a C++/CLI layer too?).

I have tried the recommendation here but access denied (admin rights required)

I also tried this Side-By-Side COM Interop with C# and VBA. But the .NET framework that excel targets is of an older version than that which I build my C# on. Not sure how I can fix this... Would it require registry edits? because if so, I can't use that method as I don't have admin rights.

Lastly I looked at this option https://pragmateek.com/using-c-from-native-c-with-the-help-of-ccli-v2/. But it appears to be going via C++ -> C++/CLR -> C# source. considering I start in VBA, that's two intermediate steps... surely there is a better way?

13
  • 1
    This sounds like an exercise in suffering. Depending on your scenario, consider going the other way and driving editing or generating the necessary Excel data from a C# application. This can even be done without having to automate Excel, though that is an option. Commented Feb 17, 2023 at 16:05
  • 2
    Your first link seem like a good tutorial as to how to make a COM visible assemby (which is the way to approach calling .Net code from Excel) what specifically was the problem you encountered? Perhaps create a new class library that interfaces with your existing codebase and make that the COM visible intermediary to keep things simple. Commented Feb 17, 2023 at 16:17
  • I think what you want is Roberts UnmanagedExports (or Repack, or DLLExport). This could be as simple as adding [DllExport] to your C# calls and Importing them into VBA. stackoverflow.com/questions/70373749/… Commented Feb 17, 2023 at 16:41
  • 1
    What about Excel DNA: excel-dna.net I personally like it, but deploying i have not tested yet. It produces .xll for both 32 and 64 bit versions. Commented Feb 17, 2023 at 17:58
  • Edit, I managed to build my c# in .NET 4.5 instead of 4.7.2 and it worked! Thanks for the link @clamchoda. Next I will be exploring c++ to c++CLR to c# and seeing how robust it feels. Commented Feb 17, 2023 at 20:21

0

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.