Trying to call a VBA object with a numeric name

时间:2017-06-15 09:27:17

标签: vba object

I have a VBA object and I'm trying to call a property of a child object

Object.key1.1.property

I'm getting an error because the name of a nested object is a number

if I use

Object.key1.[1].property

I get a runtime error 438.

Does anyone know how I can call this property?

Update: A bit of background info. The object (collection rather) is created out of a JSon string via a Json parsing macro. See snip of the locals window to understand how the collection is organised.

Based on the picture of the locals window, I've tried using:

JsonObject.data0.item(1).AccountTypeID

to get a value, but the item method doesn't work.

1 个答案:

答案 0 :(得分:0)

我不得不说我甚至无法设想在VBA中创建名称为数字的属性的方法。我不相信你可以在VBA中做到这一点,我也不知道即使你创建了自己的库也可以做到。所以最明显的问题是你确定你的财产参考是否正确?

假设存在这样一个属性的理论可能性,那么可以通过CallByName()函数在VBA中调用它,该函数可以通过其名称检索属性,作为字符串传递。

假设您想要访问ThisWorkbook.ActiveSheet.Rows.Count属性 - 也就是说,在您的术语中,所需的子对象为Rows及其所需的属性Count - 那么您可以实际上是使用以下语法执行此操作:

Dim childObj As Range

Set childObj = CallByName(ThisWorkbook.ActiveSheet, "Rows", VbGet)
Debug.Print childObj.Count

您发布的问题实在太过分了,以至于我甚至无法使用名为1的属性创建测试对象,因此我无法为您验证此问题具体的例子,但也许,只是,可能,该功能可能适合你:

Set childObj = CallByName(Object.Key1, "1", VbGet)
Debug.Print childObj.Property