Iterating through custom properties
You cannot iterate through the custom properties in a Properties or DataSourceProperties object using a For Each...Next loop. However, you can iterate through the custom properties using the Count property; for example, using a For...Next loop.
The value of the Count property (as well as the other properties of the Properties and DataSourceProperties objects) are valid only for the specified user context or the current user context if you do not specify the user context to use.
To see all of the custom properties for all possible user contexts, use a nested loop construction, with the outermost loop setting Document.Contexts.Current to each possible user context. (You can iterate through the user contexts in the Document.Contexts collection by using a For Each...Next loop.)
Save the initial value of Document.Contexts.Current property before entering the outermost loop, and set it back afterwards.
The custom properties on some objects inherit from other objects:
▪Custom properties on the VariableInstance object inherit from the associated Variable object.
▪Custom properties on the ElementInstance object inherit from the associated Element object.
▪DataSourceProperties inherit from the Properties collection on the same object.
To iterate through all of the custom properties in the Properties collection on an object that does not support inheritance of custom properties, iterate through the custom properties for the current user context using Properties.Count.
Iterating through custom properties on an object that supports inheritance of custom properties requires a two-step process. Similarly, iterating through the DataSourceProperties collection requires a two-step process, because it inherits from the Properties collection. This means that you need a four-step process to iterate through the DataSourceProperties on the VariableInstance and ElementInstance objects if you want to include the custom properties inherited from the Properties collection on the same object and the DataSourceProperties and Properties collections on the Variable and Element objects, respectively.
The following instructions show how to iterate through all of the custom properties on a VariableInstance object, including the custom properties inherited from the associated Variable object.
You can use a similar approach to iterate through the custom properties for an ElementInstance object including the custom properties inherited from the associated Element object. You can also use a similar approach to iterate through the DataSourceProperties collection to include the custom properties inherited from the Properties collection on the same object.
To iterate through all of the custom properties in the current user context in the Properties collection on a VariableInstance object
1 Using the Count property, iterate through all of the custom properties in the Properties collection of the associated Variable object and put all of these custom properties into a private list.
The list now contains the inherited custom properties only.
2 Again using the Count property, iterate through all of the custom properties in the Properties collection of the VariableInstance object and add each custom property to your private list. If a custom property already exists in the private list, overwrite it with the new value, because the custom property that was inherited has now branched.
Your private list now contains all of the custom properties (both direct and inherited) of the VariableInstance object.
See also