Excel VBA calling a .net 6 function?

SilverShaded

Well-known member
Joined
Mar 7, 2020
Messages
51
Programming Experience
10+
Apologies if this has been asked before but i couldnt find an example of this working. In the .net framework is realtively easy to get excel talking to C# code via COMinterop but i cant find anything equivelent in .Net6. Really want to move to .Net 6 as it executes my code faster but i still need excel to be able to call some functions? Are there any examples out there or does anyone know how to do it?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,190
Location
Sydney, Australia
Programming Experience
10+
.NET 5 and 6 are based on .NET Core. The following page provides information based on .NET Core 3, so I would guess that it would apply.

 

SilverShaded

Well-known member
Joined
Mar 7, 2020
Messages
51
Programming Experience
10+
.NET 5 and 6 are based on .NET Core. The following page provides information based on .NET Core 3, so I would guess that it would apply.

Hi Thanks yes thats the sort of thing i was looking for and i have the example working without to much trouble. However I then get to the point where i either need to "write C++ header file" or write an MIDL file to be able to generate a TLB file otherwise i can't call it from excel? I do not which is the easier route as i've never used MIDL or would know how to go about writing a the C++ header file and then use to get a TLB file?
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,190
Location
Sydney, Australia
Programming Experience
10+
I had no idea what an IDL file was before reading your post. I went to a search engine and typed in "how to write an idl file" and, lo and behold, it provided links to useful information. The one that immediately stood out was this:

 

SilverShaded

Well-known member
Joined
Mar 7, 2020
Messages
51
Programming Experience
10+
I had no idea what an IDL file was before reading your post. I went to a search engine and typed in "how to write an idl file" and, lo and behold, it provided links to useful information. The one that immediately stood out was this:

Thanks. I just the cold sweaty feeling im going down a blind rabbit hole with this, will give it a go though.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,029
Location
Chesapeake, VA
Programming Experience
10+
I don't know if the tools still exist, but there used to be TLB to IDL converters. You could first make your assembly in .NET Framework 4.8 and generate the TLB. Then you feed the TLB into the converter to get back an IDL. Then you update the IDL with the GUIDs from your .NET 6 code. (You can skip this last step if you are using the same GUIDs.)

Anyway, handwriting an IDL is really easy if you know C or C++.
 

SilverShaded

Well-known member
Joined
Mar 7, 2020
Messages
51
Programming Experience
10+
Finally got time to try this, taken me all day but got it working in .Net 5 eventually. Most of it was just getting the format of the IDL file correct with the right GUIDS in the right place for the class and the Interface, but took most of the day to figure out what was going on, then converting it to a typelibrary. Very fiddly really for a part time/amateur programmer like me but anyway works now and excel is calling .Net 5 with no problems (for the moment).
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,029
Location
Chesapeake, VA
Programming Experience
10+
Glad you persevered through it. You should put together a blog post.
Or better yet, write up the steps, open an issue with the MS documentation, and then offer up your text as a fix for the issue.
 
Top Bottom