VBA 可以枚举 COM 对象的方法或字段吗?

发布于 2024-12-14 20:11:39 字数 398 浏览 1 评论 0原文

我有一个 COM 对象(用 C#.NET 构建),正在 VBA (Excel) 中使用,如果能够枚举 COM 对象的字段并自动引用它们,那就太好了。在 .NET 中,这可以通过反射来完成。有没有办法在VBA中做到这一点?

所以,而不是

Dim x As MyCOMObject
Set x = New MyCOMObject
x.f1 = 1
x.f2 = 2
x.f3 = 3

更像:

Dim x As MyCOMObject
Set x = New MyCOMObject
For i = 0 to COMFieldCount(x) - 1
    SetCOMField(x, GetCOMFieldName(i), i+1)
Next i

I have a COM object (built with C#.NET) I'm using in VBA (Excel) and it would be really nice enumerate the COM object's fields and reference them automatically. In .NET, this could be done with reflection. Is there any way to do this in VBA?

So, instead of

Dim x As MyCOMObject
Set x = New MyCOMObject
x.f1 = 1
x.f2 = 2
x.f3 = 3

Something more like:

Dim x As MyCOMObject
Set x = New MyCOMObject
For i = 0 to COMFieldCount(x) - 1
    SetCOMField(x, GetCOMFieldName(i), i+1)
Next i

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把梦留给海 2024-12-21 20:11:39

您可能需要稍微改进一下这段代码,但它大致可以满足您的需求。
首先,您需要添加对“Typelib信息”TLBINF32.dll的引用。我不确定这是否是 Windows 的一部分,或者是我在计算机上安装的众多 SDK 中的一部分,但它位于 System32 文件夹中。

我假设您正在设置 COM 对象的属性,因此您将调用“property put”函数来设置对象的值。您可能需要检查这些属性的数据类型,我的代码中没有进行任何数据类型转换。

代码如下所示:

'Define the variables
Dim tliApp As TLI.TLIApplication
Dim typeinfo As TLI.typeinfo
Dim interface As TLI.InterfaceInfo
Dim member As TLI.MemberInfo

'Initialize typelib reflector
Set tliApp = New TLI.TLIApplication
'Get the type information about myObject (the COM object you want to process)
Set typeinfo = tliApp.ClassInfoFromObject(myObject)

'Set all properties of all the object's interfaces
For Each interface In typeinfo.Interfaces
    For Each member In interface.Members
        'If this is a "property put" function
        If member.InvokeKind = INVOKE_PROPERTYPUT Then
            'Invoke the mebmer and set someValue to it.
            'Note that you'll probably want to check what datatype to use and do some more error checking
            CallByName myObject, member.Name, VbLet, someValue
        End If
    Next
Next

You will probably need to refine this code a bit, but it does roughly what you are looking for.
First, you need to add reference to "Typelib information", TLBINF32.dll. I'm not sure if this is a part of Windows or came with some of the numerous SDKs I have installed on my machine, but it is in the System32 folder.

I am assuming that you are setting properties of a COM object, so you will be calling "property put" functions to set the values of your object. You may need to check for the datatypes of those properties, I haven't done any datatype conversion in my code.

The code looks like this:

'Define the variables
Dim tliApp As TLI.TLIApplication
Dim typeinfo As TLI.typeinfo
Dim interface As TLI.InterfaceInfo
Dim member As TLI.MemberInfo

'Initialize typelib reflector
Set tliApp = New TLI.TLIApplication
'Get the type information about myObject (the COM object you want to process)
Set typeinfo = tliApp.ClassInfoFromObject(myObject)

'Set all properties of all the object's interfaces
For Each interface In typeinfo.Interfaces
    For Each member In interface.Members
        'If this is a "property put" function
        If member.InvokeKind = INVOKE_PROPERTYPUT Then
            'Invoke the mebmer and set someValue to it.
            'Note that you'll probably want to check what datatype to use and do some more error checking
            CallByName myObject, member.Name, VbLet, someValue
        End If
    Next
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文