I Want You, Dynamic connector!
When I’m creating Visio-based graphical solutions, I frequently run into the situation where I want to send all of the connectors on a page to the back.
There are a number of reasons to do this, but more importantly for Visio Guy readers, there are a number of ways to do this!
In today’s article, you’ll learn a number of methods to select shapes by type or layer, and you’ll see a little bit of VBA code that will help you automate the task!
In order to send all of the connectors on a page to back, we need to create a Selection of connector shapes. Once we have a selection of shapes, we can quickly call the Send to Back command on them.
So this article is really about how to select specific groups of shapes.
For End-users: The Select by Type Feature
If you have a complicated drawing, selecting a sub-set of shapes on a page can be an excercise in Shift+Click misery.
But if you’ve ever peeked closely at the Edit menu, you may have noticed the Select by Type item:
Once you click this item, you’ll be presented with this nifty dialog:
You can see that shapes can be selected by Layer, which can save you a LOT of time. Here we can see that we can select shapes on the Connector layer in one fell swoop.
We can also select shapes that aren’t on a layer at all. This might be useful for drawings that insist that all objects be on a layer. You could quickly check if any shapes had slipped through the cracks!
Unfortunately, there is no select shape by Master, which would also be extremely useful. The select by Shape type seems to lead us in that direction, but alas, there is no Master check box. FEATURE REQUEST FOR MICROSOFT: Add Select by Master to the Select by Type dialog!
Select by Layer Code
Analogouse to the Select by Type – Layer option we saw above, you can programmatically select shapes by layer. As you might have guessed from that dialog, Visio’s built-in, Dynamic connector comes pre-assigned to the Connector layer, so we can take advantage of this in our code.
Below, our code snippet defines a Layer object that corresponds to the Connector layer. Once we have this object, we can ask Visio to return a selection of shapes on that particular layer.
And once we have a Selection of shapes, we can call SendToBack to put them behind all other shapes on the page.
Note, you’ll want to check that a layer exists in your own code, but we’ve kept the sample as simple as possible for clarity.
Sub SendConnectorsToBack_ByLayer() '// Get the active page: Dim pg As Visio.Page Set pg = Visio.ActivePage '// Get the connectors layer: '// Note, ItemU should work for non-English Visio Dim lyr As Visio.Layer Set lyr = pg.Layers.ItemU("Connector") '// Get a selection of connectors, by layer: Dim sel As Visio.Selection Set sel = pg.CreateSelection( _ visSelTypeByLayer, visSelModeSkipSub, lyr) '// Send the selection to back: If (sel.Count > 0) Then Call sel.SendToBack End If End Sub
Select by Master Code
While the Select by Type dialog didn’t offer the ability to select by Master, Visio’s automation model does! This code looks almost identical to the previous select-by-layer procedure, except that a Master object is passed to the page’s CreateSelection method, instead of a Layer object.
Sub SendConnectorsToBack_ByMaster() '// Get the active page: Dim pg As Visio.Page Set pg = Visio.ActivePage '// Get the connector master: '// Note, ItemU should work for non-English Visio. Dim mst As Visio.Master Set mst = pg.Document.Masters.ItemU("Dynamic connector") '// Get a selection of connectors, by layer: Dim sel As Visio.Selection Set sel = pg.CreateSelection( _ visSelTypeByMaster, visSelModeSkipSub, mst) '// Send the selection to back: If (sel.Count > 0) Then Call sel.SendToBack End If End Sub
Why Send Connectors to Back?
Ok, the “select special” stuff was cool enough, but you still might be wondering why I would ever waste my time sending connectors to back?
I can think of two cases right off the top of my head, so I’ll share them quickly before you get back to work.
Sometimes it is convenient to connect shapes center-to-center. But if your connectors are on top, your diagram won’t look so hot. Sending connectors to back easily remedies the situation:
Another case involves line-weight. You may have connections that attach at shape edges, thus eliminating the previous problem.
But if your connectors are thicker than your shape-outlines, you will still get problems with the line-end cap. Sending connectors to back will fix this problem too!
Of course, you can always call SendToBack each time you drop a connector on the page, or you could just be sure to drop the connectors before you drop the shapes.
But I’m sure that you’ll run into good reasons–such as performance or code-organization–to send them all to back with one, central procedure. And even if you never send a single connector to back, you’ll surely find use for the select-by-layer and select-by-master procedures we’ve talked about today!
I inadvertedly did something to a graph and now my connects will not connect to the object. I don’t get a red box to save my soul. I have tried to copy and past, I have try starting over, but nothing will change it back. Very new to Visio and lack time to really study it. Any suggestions?
Thanks! This provided the very easiest and quickest way to do what I needed. My flowchart has become complex, and I kept accidentally hiding connector ends. This was the instant way to bring them to the front.
THANKS!
Where is this Option at Visio2013?