传递参数的数组CallByName VBA [英] Passing an array of Arguments to CallByName VBA

查看:1617
本文介绍了传递参数的数组CallByName VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用callByName我VBA动态调用类的不同方法。根据不同的方法中,我将具有不同数量的,这将在阵列中保持的参数。不幸的是CallByName接受一个参数数组,因此它不是简单的通过一个可变数目。有没有解决的办法,我发现使用Type信息库的解决方案,但这似乎并没有对VBA工作,即使我将它作为参考。下面是我想要的插图

 公用Sub Initialize_Object(为ByRef TaskObject,Task_Collection) 昏暗Task_begin为Variant,Method_Parameters为Variant Task_begin = Task_Collection(方法) CallByName TaskObject,Task_begin,VbMethod,Method_Parameters


解决方案

您可以使用数组通过改变方法签名使用CallByName作为参数:

 #如果VBA7或者Win64的再
  私人声明PTRSAFE功能rtcCallByName库VBE7.DLL(_
    BYVAL对象作为对象,_
    BYVAL PROCNAME作为LongPtr,_
    BYVAL CALLTYPE作为VbCallType,_
    为ByRef ARGS()为已任,_
    可选BYVAL LCID长)为Variant
#其他
  私人声明函数库rtcCallByNameVBE6.DLL(_
    BYVAL对象作为对象,_
    BYVAL PROCNAME长,_
    BYVAL CALLTYPE作为VbCallType,_
    为ByRef ARGS()为已任,_
    可选BYVAL LCID长)为Variant
#万一公共职能CallByName2(对象为对象,PROCNAME作为字符串,ARGS()为Variant)
   AssignResult CallByName2,rtcCallByName(对象,StrPtr(PROCNAME),VbMethod,参数)
结束功能私人小组AssignResult(目标,结果)
  如果VBA.IsObject(结果)然后设定目标=结果否则目标=结果
结束小组

下面是一个使用示例:

 子UsageExample()
  昏暗的obj为对象,参数()  昏暗的obj作为新的Class1
  参数=阵列(1,3)  CallByName2 OBJ的MyMethod,论据
结束小组

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 array. Unfortunately CallByName accepts a param array, therefore it's not straightforward to pass a variable number. Is there a way around this, I found a solution using the Type Information Library but this does not seem to work on VBA even though I have added it as a reference. Below is an illustration of what I want

 Public Sub Initialize_Object(ByRef TaskObject, Task_Collection)

 Dim Task_begin As Variant, Method_Parameters As Variant

 Task_begin = Task_Collection("Method")

 CallByName TaskObject, Task_begin, VbMethod, Method_Parameters

解决方案

You could use CallByName with an array as argument by changing the method signature :

#If VBA7 Or Win64 Then
  Private Declare PtrSafe Function rtcCallByName Lib "VBE7.DLL" ( _
    ByVal Object As Object, _
    ByVal ProcName As LongPtr, _
    ByVal CallType As VbCallType, _
    ByRef args() As Any, _
    Optional ByVal lcid As Long) As Variant
#Else
  Private Declare Function rtcCallByName Lib "VBE6.DLL" ( _
    ByVal Object As Object, _
    ByVal ProcName As Long, _
    ByVal CallType As VbCallType, _
    ByRef args() As Any, _
    Optional ByVal lcid As Long) As Variant
#End If

Public Function CallByName2(Object As Object, ProcName As String, args() As Variant)
   AssignResult CallByName2, rtcCallByName(Object, StrPtr(ProcName), VbMethod, args)
End Function

Private Sub AssignResult(target, result)
  If VBA.IsObject(result) Then Set target = result Else target = result
End Sub

Here is a usage example:

Sub UsageExample()
  Dim obj As Object, arguments()

  Dim obj As New Class1
  arguments = Array(1, 3)

  CallByName2 obj, "MyMethod", arguments
End Sub

这篇关于传递参数的数组CallByName VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆