Getting the Name of Glued Connection Points

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.

 

image

 

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).

 

image

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).

10 Responses to “Getting the Name of Glued Connection Points”

  1. Matt Says:

    In addition to this functionality is there any way to populate the shape data from the connectors?

  2. Kevin Callan Says:

    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?

  3. Jørund Dahle Says:

    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.

  4. Charles Says:

    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

    • davidjpp Says:

      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

  5. Charles Says:

    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

  6. William Chow Says:

    Can you do an example implementing this with AddAdvise?

  7. anand7um Says:

    Hi David, can this be easily modified to run with Visio 2010, request you guidance. Thanks!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

davecra.wordpress.com/

Microsoft Office Development, and more...

PowerShell.Amsterdam

Automate, Accelerate, Accurate

johnvisiomvp

Life with Visio and other Microsoft Toys!

Title (Required)

Windows Server Essentials Tips & Tricks

Nilsandrey's Weblog

Just another WordPress.com weblog

Things that Should be Easy

Every so often (too often in the IT industry) I encounter things that should have been very easy to do but turned out to be far too complicated. My favorite topics include SharePoint, .Net development, and software architecture, especially distributed systems.

Visio Guy

Shapes, Stencils, Drawings Templates, Tutorials, Tips & Developer Info for Microsoft Visio

Hannes's Virtual Earth

Tips & Tricks around Mapping and Cloud Computing

Pluralsight blog

be smart, be clear, be visual ...

%d bloggers like this: