首页 > 解决方案 > 有没有办法在 VBA 运行时匹配类属性?

问题描述

我试图在运行时将从一个类中获得的属性与另一类的相同属性进行匹配。经过一番研究,我发现.net中有“反射”之类的东西,但我只使用VBA。仅作为背景:我正在使用此代码自动化应用程序,因此有些对象是公开的,而其他对象则没有。

我目前这样做的方式是使用获得的类“描述”的属性,并使用它在目标类中搜索相同的属性。

Set TargetVar = hySetOperations.Item(j).TargetVariable 'This is a RealVariable a property that refers to a class
Set SourceObj =hySetOperations.Item(j).SourceObject  'This is also a RealVariable
'In order to import variable from source object, we r gonna use TargetVariable description and truncate space, and use it (This might not work if description
'is different than actual name of the variable)
Dim RealVarString As String
RealVarString = TargetVar.Description
'Trim spaces
RealVarString = Replace (RealVarString, " ", "")
Set SourceVar = CallByName ( SourceObj, RealVarString, vbGet)

这实际上适用于大多数情况,因为“描述”通常与属性名称相同,但带有空格。但是,在某些情况下,情况并非如此,事情会向南发展。

标签: excelvbaclass

解决方案


推荐阅读