Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call non-registered .NET dll from Excel VBA

Tags:

c#

.net

excel

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?

like image 693
Christian Madsen Avatar asked May 09 '11 06:05

Christian Madsen


People also ask

How do I use .NET DLL in VBA?

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.

How do you call a DLL in VBA?

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.

Can you use C# in VBA?

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.


1 Answers

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.)

like image 148
Govert Avatar answered Oct 28 '22 10:10

Govert