Is it possible to call .NET code from Excel 2007?

Tag: excel Author: liuweiqiang0208 Date: 2009-07-11

I need to call some .NET code from Excel 2007, and I'm searching for the best way.

The official "Microsoft-sanctioned" method seems to involve Visual Studio 2008 Professional, but I don't have that. I have VS2005, but that plus VSTO 2005 SE can only do application-level add-ins, not document-level macros.

The problem involves some analysis code that comes as .NET assemblies; I'd like to manipulate it by changing some cells in an Excel worksheet.

Any other ideas?

Other Answer1

You could expose your .net objects as COM objects, then call them from VBA...

The last post in this link has some code:

http://bytes.com/groups/ms-access/205532-calling-c-net-dll-vba

Other Answer2

You could use Excel-DNA - an independent open-source project to integrate .NET into Excel. It allows creation of Excel add-ins, including high-performance user-defined functions, using managed code. It needs only .NET 2.0 or higher, so your VS 2005 will be fine.

(Disclaimer: I'm the developer of Excel-DNA.)

Other Answer3

You can (easily) call any COM components from VBA; you can also (less easily) call other DLLs etc. - even system DLLs.

If you create a COM wrapper for your .NET DLLs you'd be able to call those from VBA. You can do that with VS2005, but the amount of work involved will depend on the complexity of the DLLs' API.

Other Answer4

You may find a complete example here for calling .NET methods from excel macros: http://finaquant.com/table-function-demo-with-microsoft-excel/3788

It also demonstrates how simple parameters can be passed from excel VBA to C#/.NET