1

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Konstantin
  • 57
  • 1
  • 8
  • You probably won't be able to avoid doing some coding in VBA, I took the freedom and updatet the tag for some additional visibility. Also, please update the question with what you've already tried and the code/"actions" you already have. – L8n Jun 24 '19 at 12:42

1 Answers1

1

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.

L8n
  • 728
  • 1
  • 5
  • 15
  • Thanks for the detailed example. If I understood you correctly, I have to add that code to each new document (Visio-file) where I will use my set of shapes. Or I could add your VBA code (from an example above) right to VSSX file? – Konstantin Jun 25 '19 at 10:36
  • In theory you can add the `Private WithEvents vsApp As Visio.Application` to any module (this includes code in the stencils). To be able to store code in the stencil it must be saved as a `.vssm` document! Just keep in mind, that the code will only run if the stencil is loaded. – L8n Jun 25 '19 at 11:22
  • I just tested the code and it seems to run fine even if it is stored in the `ThisDocument` of the Stencil. You will have to delete/alter the `If Not Connects.Document Is Me.DocumentSheet Then Exit Sub` lines, since the event will be raised from the "Visio-Document" and not from the "Visio-Stencil". This was only a safeguard against the possibility that someone has two documents open in the same application... – L8n Jun 25 '19 at 11:24
  • The proper way to Implement this would probably be a separate class that listens to the events of the Application and can hold a reference to the Document it is supposed to watch.... – L8n Jun 25 '19 at 11:28