Referencing collections in macros
Referencing collections in macros
Many objects are members of collections of objects. A collection is similar to an array, except that it contains objects rather than values. Despite this, members of collections can be accessed in the same way as arrays. For
example, a collection that is used frequently in CorelDRAW is the collection of shapes on a layer: the object ActiveLayer references either the current layer or the layer that is selected in the CorelDRAW Object Manager docker.
CorelDRAW contains many collections: a document contains pages, a page contains layers, a layer contains shapes, a curve contains subpaths, a subpath contains segments and nodes, a text range contains lines and words,
a group contains shapes, and the application contains windows. All of these collections are handled in the same way by VBA.
For detailed information on the CorelDRAW object model, see the Appendix.
Referencing the items in a collection
To reference the shapes on a layer, that layer’s collection of shapes is used: ActiveLayer.Shapes . To reference the individual shapes in the collection, the Item() property is used:
Dim sh As Shape Set sh = ActiveLayer.Shapes.Item(1)
Most elements of a collection start at 1 and increase. For the collection ActiveLayer.Shapes , Item(1) is the item at the “top” or “front” of the layer — in other words, the one that is in front of all the other shapes.
Because each item in the ActiveLayer collection is an object of type Shape , you can reference the item’s members merely by appending the appropriate dot-notated member:
ActiveLayer.Shapes.Item(1).Outline.ConvertToObject Sometimes, individual items have names. If the item you are looking for has an associated name (and you know
what the name is and which collection the item is in), you can reference the item directly by using its name: Dim sh1 As Shape, sh2 As Shape Set sh1 = ActiveLayer.CreateRectangle(0, 5, 7, 0) sh1.Name = "myShape" Set sh2 = ActiveLayer.Shapes.Item("myShape")
Also, because an item is usually the implied or default member of a collection, it is not strictly required, so the last line of the preceding code can be rewritten as follows:
Set sh2 = ActiveLayer.Shapes("myShape")
Counting the items in a collection
All collections have a property called Count . This read-only property gives the number of members in the collection:
Dim count As Long count = ActiveLayer.Shapes.Count
The returned value is not only the number of items in the collection, but because the collection starts from 1 , it is also the index of the last item.
Parsing the items in a collection
It is often necessary to parse through the members of a collection to check or change the items’ properties. By using the Item() and Count members, it is straightforward to step through a collection of items. With each
iteration, it is possible to test the current item’s properties, or to call its methods. The following code restricts all shapes on the layer to no wider than ten units:
Dim I As Long, count As Long count = ActiveLayer.Shapes.Count For I = 1 to count
If ActiveLayer.Shapes.Item(i).SizeWidth > 10 Then ActiveLayer.Shapes.Item(i).SizeWidth = 10 End If Next I There is, however, a more convenient way of parsing a collection in VBA. Instead of using the Count property
and a For-Next loop, this technique uses a For-Each-In loop: Dim sh As Shape
For Each sh In ActiveLayer.Shapes If sh.SizeWidth > 10 Then sh.SizeWidth = 10 End If Next sh
If you want to copy the selection and then parse it later when it is no longer selected, copy the selection into a ShapeRange object:
Dim sr As ShapeRange Dim sh As Shape Set sr = ActiveSelectionRange For Each sh In sr
' Do something with each shape Next sh