Passing an array of Arguments to CallByName VBA

后端 未结 2 414
生来不讨喜
生来不讨喜 2020-12-21 12:53

I\'m using callByName I VBA to dynamically call different methods of a class. Depending on the method, I will have a different number of arguments which will be held in an a

2条回答
  •  心在旅途
    2020-12-21 13:57

    You can't do this dynamically because different methods will require a different amount of arguments and you can't pass arguments where they aren't expected.

    If you know the amount of arguments required, then you could call each item of the array and pass that:

    CallByName TaskObject, Task_begin, VbMethod, Method_Parameters(0), Method_Parameters(1), Method_Parameters(2)
    

    but you would probably have to set up a Select Case block or similar to handle all the different methods:

    Select Case Method_Name
        Case "Method_1": CallByName TaskObject, Task_begin, VbMethod, Method_Parameters(0), Method_Parameters(1)
        Case "Method_2": CallByName TaskObject, Task_begin, VbMethod, Method_Parameters(0)
        Case "Method_3": CallByName TaskObject, Task_begin, VbMethod, Method_Parameters(0), Method_Parameters(1), Method_Parameters(2)
    End Select
    

    Which can get messy quite easily.

提交回复
热议问题