Search code examples
vbadllcom

VBA importing COM-registered dll and calling constructor with arguments


Is it possible to reference a COM-registered DLL and then create objects that require arguments in the constructor in VBA code?

I'm successfully referenced the tlb in Access 2013 (64-bit) that was registered with regasm, and I've managed to create a simple object that doesn't need arguments in the constructor.

From what I read, it is not. Do I now face major refactoring?


Solution

  • It doesn't have to be "major". You simply need a factory method, one that takes all the arguments you need to construct the object. For example:

    public class Foo {
        internal Foo(int arg1, string arg2) {
           // etc...
        }
        // Other methods and properties
        //...
    }
    
    public class FooFactory {
        public Foo Create(int arg1, string arg2) {
            return new Foo(arg1, arg2);
        }
    }
    

    And now you simply use FooFactory.Create() in your VBA code.