Click here to Skip to main content
13,150,178 members (59,920 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello everyone,

I need to build a C# dll and to call it from VBA. I've tested it on a simple example with a method that returns strings. But how can I return more complex objects? For instance, my method will return a C# proprietary binary tree. How will it be converted to VBA?

Thanks in advance,
Posted 4-Sep-12 1:33am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

So basically you want your C# dll to populate a binary tree that you can then interrogate from VBA.
You will probably have to implement methods such as Load, First, Next, Count etc. and use them from VBA to get the data out of the DLL. I don't think you can use a C# proprietary binary tree for much in VBA, but I will probably stand corrected shortly.

You may get some hints from this answer.
installing and Refereing .net dll programmatically from vba app[^]
You've basically got to create a COM dll. I've had various issues trying to do so with .NET in the past - and I've resorted to make my dll in Delphi, because it just worked without any of the hoops.
losmac 4-Sep-12 16:19pm
Good answer! +5
Erik Rude 5-Sep-12 7:12am
mark4 6-Sep-12 8:09am
I just did a test with a method that returns a complex C# class. When VBA tries to call it, it generates an error ("Undefined object"). So, my question is still open...
Erik Rude 7-Sep-12 4:21am
If you share your code and experiences you may get a better response.
mark4 7-Sep-12 7:59am
My question is not linked with a specific code, suppose my mehod returns a binary tree (a class Node). The idea is to write a COM component that can be shared between applications. But I find the tutorials quite difficult and I'm looking for a simple way to do it. If you know a simple tutorial, it can help me.
Erik Rude 7-Sep-12 13:04pm
Sorry that I'm no genius :) - I did try for a while to achieve COM compatibility between .NET and Excel VBA, but I gave up in the end. I don't think MS is trying to make it easy for you. You can probably develop a whole application the integrates Excel into C# easier than the other way round. My only simple trick that I have tried was to use Delphi - but then I knew and had Delphi already so it may not be easy for you.

If you share your ultimate goal in your question you may get a better response. (E.g. why do you want to do the step you want to do? Perhaps people will tell you not to go on that path at all..)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170924.2 | Last Updated 7 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100