gpt4 book ai didi

arrays - 将参数数组传递给 CallByName VBA

转载 作者:行者123 更新时间:2023-12-04 00:39:07 25 4
gpt4 key购买 nike

我正在使用 callByName I VBA 动态调用类的不同方法。根据方法的不同,我将拥有不同数量的参数,这些参数将保存在一个数组中。不幸的是 CallByName 接受一个 param 数组,因此传递一个变量数字并不简单。有没有办法解决这个问题,我找到了一个使用类型信息库的解决方案,但这似乎不适用于 VBA,即使我已将其添加为引用。下面是我想要的插图

 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

最佳答案

您可以通过更改方法签名将 CallByName 与数组一起用作参数:

#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

这是一个使用示例:
Sub UsageExample()
Dim obj As Object, arguments()

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

CallByName2 obj, "MyMethod", arguments
End Sub

关于arrays - 将参数数组传递给 CallByName VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36313575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com