Connecting Shapes in a Flowchart: an Example

The following Microsoft® Visual Basic® for Applications (VBA) procedure draws a simple flowchart on the Microsoft® Visio® drawing page based on data contained in an array that is passed as an argument to this procedure. The array is two-dimensional—the first element contains the name of the master, and the second element contains the shape's text. Before creating the flowchart, the procedure reads the array data and prints it to the Immediate window of the Visual Basic Editor.

Flowchart created by CreateFlowchart program

The flowchart created by CreateFlowchart

Public Sub CreateFlowchart(arrFlowChart () As String)     'Shape that you're connecting from     Dim vPrevShape As Visio.Shape     'Shape that you're connecting to     Dim vNextShape As Visio.Shape     'Shape representing the connection     Dim vConnector As Visio.Shape     'Reference to the flowchart master     Dim vFlowChartMaster As Visio.Master     'Reference to the connector master     Dim vConnectorMaster As Visio.Master     'The stencil containing the masters     Dim vStencil As Visio.Document     'Master's PinX location     Dim dblXLocation As Double     'Master's PinY location     Dim dblYLocation As Double     'Begin cell for connector     Dim bCell As Visio.Cell     'End cell for connector     Dim eCell As Visio.Cell     Dim iCount As Integer

    On Error GoTo eHandler     'Initialize X,Y location that will be passed to the Drop method     dblXLocation = 4.25     dblYLocation = 10.5     'Print array to Immediate window     For iCount = LBound(arrFlowChart) To UBound(arrFlowChart)         Debug.Print arrFlowChart(iCount, 0) & " "; arrFlowChart(iCount, 1)     Next     'Open Flowchart stencil     Set vStencil = Application.Documents.OpenEx _         ("Basic Flowchart Shapes.vss", visOpenDocked)     'Add a shape to the drawing for each item in the array     For iCount = LBound(arrFlowChart) To UBound(arrFlowChart)         'Get the master based on MasterName in the data array         Set vFlowChartMaster = vStencil.Masters(arrFlowChart(iCount, 0))         'Add the shape to the page         Set vNextShape = ActivePage.Drop(vFlowChartMaster, dblXLocation, _             dblYLocation)         'Set dropped shape text         vNextShape.Text = arrFlowChart(iCount, 1)         'Connect to previous shape dropped, if necessary         If Not vPrevShape Is Nothing Then             'Get connector master if necessary             If vConnectorMaster Is Nothing Then                 Set vConnectorMaster = vStencil.Masters("Dynamic Connector")             End If             'Add connector to the page (doesn't matter where for this example)             Set vConnector = ActivePage.Drop(vConnectorMaster, 0, 0)             'Connect begin point             Set bCell = vConnector.Cells("BeginX")             bCell.GlueTo vPrevShape.Cells("AlignBottom")             'Connect end point             Set eCell = vConnector.Cells("EndX")             eCell.GlueTo vNextShape.Cells("AlignTop")             vConnector.SendToBack         End If         Set vPrevShape = vNextShape         Set vNextShape = Nothing         'Set Y location for next shape         dblYLocation = dblYLocation - 1.5     Next     Exit Sub     eHandler:     Debug.Print Error End Sub