Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# COM VBA
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,
Mark
Posted 4-Sep-12 2:33am
mark4128

1 solution

Rate this: bad
good
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.
  Permalink  
v2
Comments
losmac at 4-Sep-12 16:19pm
   
Good answer! +5
Erik Rude at 5-Sep-12 7:12am
   
Thanks!
mark4 at 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 at 7-Sep-12 4:21am
   
If you share your code and experiences you may get a better response.
mark4 at 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 at 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
0 OriginalGriff 274
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 PIEBALDconsult 200
4 BillWoodruff 174
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 7 Sep 2012
Copyright © CodeProject, 1999-2014
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