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).
October 4, 2013 at 2:29 pm
In addition to this functionality is there any way to populate the shape data from the connectors?
October 4, 2013 at 2:40 pm
Try : http://blog.bvisual.net/2009/02/02/copying-data-from-one-shape-to-another/
And maybe :
http://blog.bvisual.net/2012/10/15/adding-more-smartness-to-visio-connectors/
http://blog.bvisual.net/2012/10/13/a-visio-logic-gate-with-logic/
http://blog.bvisual.net/2009/09/16/listing-connections-in-visio-2010/
January 28, 2015 at 7:37 pm
I really enjoyed the post on connectors–so much so that I have been trying to walk through it on my own. Unfortunately, I am not able to even generate the block shape with the names connection points. How do you name connection points and display then on the block?
May 4, 2015 at 10:17 am
Hi.
I tried your VBA, and it worked very well! I am not a programmer, so I do not understand everything you did, but I have an idea to use this method to be able to make a signal report based on the shape data in all connectors on a drawing. I therefore wondered if there was possible to add the connected shape information (From/To) into a connectors shape data? That is, a “From shape” and a “To shape” shape data.
May 26, 2015 at 8:34 pm
Hi there,
I’m very new to programming with Visio and I would like to know how you implement this code? It will be very useful for me!
Thank you
May 26, 2015 at 9:39 pm
You can use ALT+F11 to get to the VBA Editor. The code needs to be in ThisDocument, and you need to save the file as macro-enabled in Visio 2013
June 8, 2015 at 5:07 pm
Great, got it working. Thanks!
Would there be a way to change the colour of the line based on what the text inside the cell is?
Thanks,
Charles
January 4, 2016 at 3:31 am
Can you do an example implementing this with AddAdvise?
February 28, 2017 at 11:45 pm
Hi David, can this be easily modified to run with Visio 2010, request you guidance. Thanks!
March 1, 2017 at 1:21 pm
That code will work with all versions and editions of Visio