0

I've created a class library DLL and wrapped it up, but when I use it within VBA (Excel) it has no methods. All is well from another .NET program:

This is my code:

    namespace NeilLibrary
    {
        public interface INeilTest
        {
            string DoMethodReturn(string name);
            string DoMethodOut(string name, out string name2, out bool isPrev);
        }
        [Guid("BA7CC0F2-9C07-4EF9-B799-18D317B7E293")]
        [ComVisible(true)]
        public class NeilTest
        {
            public NeilTest() { }
            [ComVisible(true)]
            public string DoMethodReturn(string name)
            {
                return "name: " + name;
            }

            [ComVisible(true)]
            public string DoMethodOut(string name, out string name2, out bool isPrev)
            {
                name2 = "New Value";
                isPrev = true;
                return "Name: " + name;
            }
        }        }

I built it with COM enabled in the assembly and I ran the following command:

regasm.exe NeilLibrary.dll /tlb:neil.tlb

Which worked fine. In Excel I imported the tlb and it appears in the object browser so it's created ok, but there are no methods. The interface is shown though, with it's methods, but that's not much use.

Can somebody help please.

I added the no-arg constructor because I read somewhere with someone with a similar problem had to do this.

Neil Walker
  • 6,400
  • 14
  • 57
  • 86
  • Have you tried [this](http://stackoverflow.com/q/11570262/1188513) and [this](http://stackoverflow.com/a/29783710/1188513)? – Mathieu Guindon Nov 25 '15 at 20:09
  • Please read my response on your other post.....http://stackoverflow.com/questions/33923624/cant-add-a-reference-to-the-specified-file/33923711?noredirect=1#comment55610787_33923711 – Seth Kitchen Nov 25 '15 at 20:10
  • COM types don't have "constructors", that's why you can't expose a managed type to COM if it only has a parameterized constructor - actually you *can*, but your COM client won't be able to instantiate it. ...and before you hit that wall, you can't expose generic types to COM either. =) – Mathieu Guindon Nov 25 '15 at 20:25

2 Answers2

2

Oh I see the difference is now you posted your code....

You need to make functions you want to use COM Visible. You can do that in a properties file or above you class like this

[ComVisible(true)]
[Guid("blah")]
public class ConnectionUtilities: IConnectionUtilities
{
}

Try making your entire Assembly ComVisible by going into your C# Project's properties->AssemblyInfo.cs and declaring like this:

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(true)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("0a2d43c9-da5e-4a59-8006-ff131b33d86c")]
Seth Kitchen
  • 1,526
  • 19
  • 53
  • 1
    thanks, I've updated my code above, but still no joy. Does it need an Interface and Interface type, etc setup? tbh, I thought it would be a simple job, but I can't find anything that takes you through this. – Neil Walker Nov 25 '15 at 20:43
  • @NeilWalker You made your methods com visible but not your class. You shouldn't need any of the other stuff. – Seth Kitchen Nov 25 '15 at 20:50
  • Still doesn't work. Your assembly details are already there, I can see the class ok, just not the methods, and I've added an interface and this is visible with the methods in VBA, just not the class. Thanks for your patience :) – Neil Walker Nov 25 '15 at 21:07
2

Firstly, thanks for everyone's help.

The solution was rather simple in the end. I simply had to add '/codebase' to the regasm.exe (or sign it and add it via gacutil).

Also, I haven't checked any other permutation, but this is the only way it worked for me:

namespace NeilLibrary
    {
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        public interface INeilTest
        {
            [DispId(1)]
            string DoMethodReturn(string name);
            [DispId(2)]
            string DoMethodOut(string name, out string name2, out bool isPrev);
        }

        [Guid("BA7CC0F2-9C07-4EF9-B799-18D317B7E293")]
        [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.None)]
        [ProgId("NeilLibrary.NeilTest")]
        public class NeilTest : INeilTest
        {
            [ComVisible(true)]
            public string DoMethodReturn(string name)
            {
                return "name: " + name;
            }

            [ComVisible(true)]
            public string DoMethodOut(string name, out string name2, out bool isPrev)
            {
                name2 = "New Value";
                isPrev = true;
                return "Name: " + name;
            }
        }
    }
Neil Walker
  • 6,400
  • 14
  • 57
  • 86