Konstantin
Konstantin

Reputation: 67

Autodetection of shapes' types of connected neighbours for Microsoft Visio 2013 / 16

I have created my own list of master shapes and put it on VSS-file. There is a shape-connector to connect the rest ones. This connector has some lines at Action Section.

I have looked through many sites but found nothing about the possibility to detect a connection event and calling the needed line from the Action Section based on types of connected shapes.

I also haven't found any section or VBA functions that could describe neighbours or existing connections.

the idea is to change the connector parameters to show the right type of connection.

Upvotes: 1

Views: 567

Answers (1)

L8n
L8n

Reputation: 728

I have created my own list of master shapes and put it on VSS-file

You really should be using .vssx by now!

I have looked through many sites but found nothing about the possibility to detect a connection event....

There is no Event for added connections on this the ThisDocument Level, but there is one on the application level.
So you can add an application object to your ThisDocument and listen to event of it. This may cause unforeseen memory leaks since it references it's parent! Description here
Someone else may be able to shed some light on this...

In the ThisDocument Code-Behind:

Option Explicit

Private WithEvents vsApp As Visio.Application


Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
    OnStart
End Sub

Private Sub Document_BeforeDocumentClose(ByVal doc As IVDocument)
    OnStop
End Sub

Public Function Kickstart()
    'Call this function to restart in case the program stops
    OnStart
End Function

Private Sub OnStart()
    Set vsApp = Me.Application
End Sub

Private Sub OnStop()
    Set vsApp = Nothing
End Sub


Private Sub vsApp_ConnectionsAdded(ByVal Connects As IVConnects)
   If Not Connects.Document Is Me.DocumentSheet Then Exit Sub 'make sure only to listen to events happening in this document
    Dim connect As connect
    For Each connect In Connects
        Debug.Print "Added Connection: ", connect.FromSheet & " To " & connect.ToSheet
    Next connect
End Sub
Private Sub vsApp_ConnectionsDeleted(ByVal Connects As IVConnects)
   If Not Connects.Document Is Me.DocumentSheet Then Exit Sub 'make sure only to listen to events happening in this document
    Dim connect As connect
    For Each connect In Connects
        Debug.Print "Deleted Connection: ", connect.FromSheet & " To " & connect.ToSheet
    Next connect
End Sub

...and calling the needed line from the Action Section based on types of connected shapes

To find the type connection/shape there are a ton of possibilities. My Go-To method is to use user-defined cells in my shapes to identify them/check if they should provoke an action. From there on you can use a select case block.

I also haven't found any section or VBA functions that could describe neighbours or existing connections.

The last two are especially interersting since you can apply filters to them.

Upvotes: 1

Related Questions