VBA importing COM-registered dll and calling constructor with arguments

青春壹個敷衍的年華 提交于 2019-12-24 14:44:26

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/32810787/vba-importing-com-registered-dll-and-calling-constructor-with-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!