Share via


Iterating through the Connections on a Page: an Example

The ShowPageConnections macro below iterates through the Connect objects for the first page in the active Microsoft® Visio® document. For each Connect object, ShowPageConnections retrieves the shapes that are connected (FromSheet and ToSheet) and the part of each shape that is connected (FromPart and ToPart). It then compares the values of FromPart and ToPart to each possible value, using the constants from the Visio type library, and displays the corresponding string, along with other data for the connection, in a listbox on a user form.

Sub ShowPageConnections ()     'Pages collection of document     Dim pagsObj As Visio.Pages     'Page to work on     Dim pagObj As Visio.Page     'Object From connection connects to     Dim fromObj As Visio.Shape     'Object To connection connects to     Dim toObj As Visio.Shape     'Connects collection     Dim consObj As Visio.Connects     'Connect object from collection     Dim conObj As Visio.Connect     'Type of From connection     Dim fromData As Integer     'String to hold description of From connection     Dim fromStr As String     'Type of To connection     Dim toData As Integer     'String to hold description of To connection     Dim toStr As String

    'Get the Pages collection for the document     'Note the use of ThisDocument to refer to the current document     Set pagsObj = ThisDocument.Pages     'Get a reference to the first page of the collection     Set pagObj = pagsObj(1)     'Get the Connects collection for the page     Set consObj = pagObj.Connects     'Make sure the listbox is empty     UserForm1.ListBox1.Clear     'Loop through the Connects collection     For Each conObj In consObj         'Get the From information         Set fromObj = conObj.FromSheet         fromData = conObj.FromPart         'Get the To information         Set toObj = conObj.ToSheet         toData = conObj.ToPart         'Use fromData to determine type of connection         If fromData = visConnectError Then             fromStr = "error"         ElseIf fromData = visNone Then             fromStr = "none"         'Test fromData for visRightEdge,visBottomEdge,visMiddleEdge,         'visTopEdge,visLeftEdge,visCenterEdge, visBeginX, visBeginY, visBegin, visEndX,         'visEndY,visEnd         ElseIf fromData >= visControlPoint Then             fromStr = "controlPt_" & CStr(fromData - visControlPoint + 1)         Else             fromStr = "???"         End If         'Use toData to determine the type of shape the connector is connected to         If toData = visConnectError Then             toStr = "error"         ElseIf toData = visNone Then             toStr = "none"         ElseIf toData = visGuideX Then             toStr = "guideX"         ElseIf toData = visGuideY Then             toStr = "guideY"         ElseIf toData >= visConnectionPoint Then             toStr = "connectPt_" & CStr(toData - visConnectionPoint + 1)         Else             toStr = "???"         End If         'Add the information to the listbox         UserForm1.ListBox1.AddItem "from " & fromObj.Name & " " & fromStr & " to " & _             toObj.Name & " " & toStr     Next     UserForm1.Show End Sub