Creating a Collection of Shapes which I can pass around

Started by adstar22, February 18, 2016, 06:02:14 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

adstar22

Normally I use arrays, but I wanted to be clever, and identified that I can add and remove items easily with a collection. But.. when I add a shape to my collection, all it adds is a string referring to the shape ID... and NOT the shape object which is what I need!


Dim shpActive As Visio.shape
Dim colDeleteShapes As Collection
Set colDeleteShapes = New Collection

Call Selection.GetIDs(lngItems)         'get the id numbers of the selected shapes, this function extracts the IDs into an array.
                                        'This enables us to identify each shape individually

For lngIDs = LBound(lngItems) To UBound(lngItems)   'loop through each of the shape IDs
    Set shpActive = ActiveWindow.Page.Shapes.ItemFromID(lngItems(lngIDs))
   
    If UCase(shpActive.NameU) <> "DYNAMIC CONNECTOR" Then
        colDeleteShapes.Add (shpActive)
    End If
Next



I intend (try) to pass the collection into another function, which then tries to use the shape object to do some other checks and stuff, before returning a different collection, which I will then compare with this one... unless I am being stupid, and can use the same collection in different funcitons (just thought of that!).. only I dont think I know how to do it... yet!!

Any ideas will be appreciated, because otherwise I will be spending tomorrow recoding a whole bunch of functions to work with a dynamic array instead!

Yacine

Try using a selection object to store the desired shapes.
Yacine

adstar22

Im not sure what you mean a "selection object"?

Maybe some further background might help, but the the code is part of the querycancelselectiondelete event. I want to be able to filter out specific shapes which can be safely deleted, or perform other activities on other shapes like removing sections from a related document, before deleting the slected shape.

This is my I was trying to use a collection. My thought was I could loop through the selected items, and "collect" up the shapes I need to analyse further.. and send the collection to the various analysis functions and routines. The end result would be a "return collection" of shapes which have been deleted... which I would then compare with the selected shapes... with the view of deselecting anything which was not deleted.

I know I can use a dynamic array to do this. But i was enticed by the beauty of it being apparently easier to add and remove items from a collection.. although in practice.. it seems to not be the case!

Yacine

Hi adstar, I meant a visio selection

dim sel as selection

instead of "colDeleteShapes.Add (shpActive)"
write: activewindow.selection. select shpActive visselect

after leaving the loop: set sel = active.window.selection

someVar = foo(sel)

Yacine

jw76novice

The VBA Collection object can contain anything in VBA - strings, integers, and even Objects.  The same collection can even hold objects of different data types, which isn't possible with arrays.  To add or retrieve an item from a Collection, though, you typically do use a string as the key for the item, but the entire item/object should be stored/retreived.  Haven't tested this in Visio, but VBA is VBA.

References:
Collection Object (Visual Basic) - MSDN - Microsoft
Add, Delete, and Retrieve Items of a Collection (Visual Basic)
Visio 2013 Professional

jwebber89

I'm facing an identical issue but the posts here haven't really helped me solve thus far.

Can anyone elaborate on the solutions posted here?

AJD

Simple error in the code:
colDeleteShapes.Add (shpActive)
should be
colDeleteShapes.Add shpActive

The "(...)" evaluates the expression when used inline like you have. So, in this case it is returning the default value (the ID). If you leave the "()" out, it will pass a reference to the shape instead.