If I define a class in a C#/.NET class library, then by making it COM visible I can instantiate the class and call its methods from VBA using COM.
Is there any way to call the static methods of such a class from VBA?
If I define a class in a C#/.NET class library, then by making it COM visible I can instantiate the class and call its methods from VBA using COM.
Is there any way to call the static methods of such a class from VBA?
COM does not support static methods, and instances of COM objects do not invoke static methods. Instead, set ComVisible(false)
on your static method, then make an instance method to wrap it:
[ComVisible(true)]
public class Foo
{
[ComVisible(false)]
public static void Bar() {}
public void BarInst()
{
Bar();
}
}
Or just make the method instance instead of static and forget static all together.
You don't have to mark the static method as not visible to COM, however it satisfies some code analysis tools that would warn you about static methods on COM visible types, and makes it clear that the static method is not intended to be visible to COM.