I need to write a .NET dll that can be called directly from an Excel's VBA Module such that the dll and .xls are simply deployed in the same directory without any COM registration.
Purpose of dll is to run algorithms implemented in C.
How should I proceed? Is this possible at all?
Using the Code In Visual Studio, create a new Class Library project. In the project Properties, click on the Application tab, click on the Assembly Information button, check the option Make assembly COM-Visible. In the Build tab, check the option Register for COM interop. Add a new class CSharpInteropService.
Call the DLL command using CALL on an XLM macro sheet, and access it via an XLM macro. Use an XLM or VBA command to call the XLM REGISTER function, which provides the information Excel needs to recognize the command when it is entered into a dialog box that expects the name of a macro command.
You can expose code in a Visual C# project to Visual Basic for Applications (VBA) code if you want the two types of code to interact with each other. The Visual C# process is different from the Visual Basic process.
You don't explain what the role of .NET would be in your scenario. You can indeed call many C libraries directly from VBA using 'Declare Function'.
If you find .NET useful, and want to call your .NET library functions as user-defined functions (UDFs) from Excel, you can use Excel-DNA. It gives you an .xll add-in library that integrates your .NET library into Excel. You'd still have to open the .xll add-in somehow - either by File -> Open, by adding it as an Excel add-in, or automatically from some VBA in your workbook. But it needs no other registration.
From the .NET library you can call the C .dll functions using P/Invoke, add categories, function and argument descriptions to integrate into the function wizard etc.
(Disclaimer: I'm the developer of Excel-DNA.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With