Unable to access COM exposed methods in VBA

The Excel object browser screenshot clearly shows a problem. Note how the GetHashcode, GetType and ToString methods are visible. Those are methods that are inherited from System.Object. But your snippet explicitly (and correctly) uses [ClassInterface(ClassInterfaceType.None)] so that the class implementation is hidden.

It isn't hidden. Not so sure how that happened, an old type library from an earlier attempt could explain it. But your build steps are very suspect, you are helping too much. The "Make assembly types COM visible" option is a pretty crude way to force the build system to expose .NET types. But your code is using the refined upper-right-pinky-up-when-you-drink-tea way to expose types to COM. Which includes the [ComVisible(true)] attribute, what the checkbox does, and the [ClassInterface] attribute, which is what the checkbox doesn't do.

So the problem is that you asked the build system to implement two interfaces. Whatever it inherited from the base class, _Object in your case, plus what it inherited from the declaration, IMyGetString in your case. Which is fine and all quite COM compatible, but VBA isn't a very good COM consumer. It only likes the [Default] interface and that's _Object in your case. Clearly visible from the screenshot.

So turn off the "Make assembly COM visible" option.

And choose between either a postbuild event that calls Regasm or the "Register for COM interop" checkbox. Doing it both ways just doubles the odds that you don't know why it doesn't work. You only need the postbuild when you need to register the assembly for the 64-bit version of Office.


I was having the same issue. I found this link.

Guide to calling a .net library from excel If I am referencing a TLB file called "Foo.tlb" which has a class called "FooClass" that has public members. You will not be able to see those members because they are built by default with late binding (run-time binding) interface only.

You can change your class to have early binding, which will allow for intellisense and the Members of the class to be exposed in the Object browser.

C#:

[ClassInterface(ClassInterfaceType.AutoDual)]
public class FooClass
{
    public FooClass()
    {
    }
    public string DoSomething()
    {
        return "I am printing....";
    }
}

You should be able to see the DoSomething method in the object browser after you implement this change. Note: You will have to deference the dll first.


For the record - in case anyone else needs this answer - I just had the exact same issue as this (OLEView and everything) and it was driving me nuts. I couldn't figure out what I was doing wrong, because I've created several C# COMs before. I forgot to declare the interface (in this post that would be IGetMyString) as public.

Hope this saves someone some time.