A Visio developer asked me if it is possible to get the name of connection points that a connector is glued to in Visio. Well, it is not possible directly from the ShapeSheet, but is easy if you use a little code.
First of all, I created a simple Block shape with 3 named connection points on either side of it. I added a little text to the block to display the shape name, and to remind me where the named connection points are.
I then added some VBA code into the ThisDocument class of the Visio document.
Note that I am using WithEvents for this example, but in real life you should not use it because it is too chatty … you should use AddAdvise instead.
By default, a Visio document comes with a Document object that has events, and I have used the DocumentOpened and PageChanged events to set a new m_page object.
The m_page object has the ConnectionsAdded and ConnectionsDeleted events in order to update the text of the connector shapes.
Option Explicit Private WithEvents m_page As Visio.Page Private Sub Document_DocumentOpened(ByVal doc As IVDocument) Set m_page = Visio.ActivePage End Sub Private Sub Document_PageChanged(ByVal Page As IVPage) Set m_page = Visio.ActivePage End Sub Private Sub m_page_ConnectionsAdded(ByVal Connects As IVConnects) Dim cnct As Visio.Connect For Each cnct In Connects If cnct.FromSheet.OneD Then AddConnectorText cnct.FromSheet End If Next End Sub Private Sub m_page_ConnectionsDeleted(ByVal Connects As IVConnects) Dim cnct As Visio.Connect For Each cnct In Connects If cnct.FromSheet.OneD Then AddConnectorText cnct.FromSheet End If Next End Sub Private Sub AddConnectorText(ByVal shp As Visio.Shape) 'A connector shape can be connected to shapes at either end 'The connector shape is always the FromSheet Dim txtBegin As String Dim txtEnd As String txtBegin = "" txtEnd = "" Dim cnct As Visio.Connect Dim connectorShape As Visio.Shape For Each cnct In shp.Connects If cnct.FromCell.Name = "BeginX" Then txtBegin = txtBegin & cnct.ToSheet.Name If cnct.ToCell.Name = "PinX" Then txtBegin = txtBegin ElseIf cnct.ToCell.RowName = "" Then txtBegin = txtBegin & " - " & cnct.ToCell.Row Else txtBegin = txtBegin & " - " & cnct.ToCell.RowName End If Else txtEnd = txtEnd & cnct.ToSheet.Name If cnct.ToCell.Name = "PinX" Then txtEnd = txtEnd ElseIf cnct.ToCell.RowName = "" Then txtEnd = txtEnd & " - " & cnct.ToCell.Row Else txtEnd = txtEnd & " - " & cnct.ToCell.RowName End If End If Next shp.Text = txtBegin & " > " & txtEnd End Sub Public Sub StartCode() Set m_page = Visio.ActivePage End Sub Public Sub StopCode() Set m_page = Nothing End Sub
I have also included two sub routines to StartCode and StopCode, for debug purposes.
Note that a connector shape can be connected at none; one or both ends, and that it can be connected to a connection point (Static Glue) or to a shape (Dynamic Glue).
In addition, a connection point row does not need to have a name, so I just display the row number (zero based).
Note that the screenshot above is from Visio 2013 … you can tell because the ends of the connector shape are green when you are about to connect … in previous versions of Visio, it was coloured red (this confusing some users who thought red means a warning).