Sub DisplayPredSuccForSingleNode()
'Given a 3-column array (Center, To, From) with a header row
'H1, H2, H3 Plot as follows
'C , T0, F0 T0 F0
'C , T1, F1 T1 F1
'C , T2, F2 T2 C F2
'C , T3, F3 T3 F3
'C , T4, F4 T4 F4
'All C values the same
'If the error "User-defined type not defined' is displayed you must add a
'Reference to the Visio Application to the VBA Environment as follows:
'1) From the Microsoft Visual Basic window menu: Tools | References
'2) Scroll to and select 'Microsoft Visio x.x Type Reference Library
'3) Click OK.
'4) From the Microsoft Visual Basic window menu: File | Close and Return to Microsoft Excel
'Start code with the worksheet containing the array as the Active Worksheet
'======================================================================================================
'======================================================================================================
Const lOutputGridMaxVerticalCount As Long = 24 'Max number of rows on either side of central element
' If this number is increased then the height of each
' rectangle (sngShapeHeight) must be reduced.
Const sngShapeHeight As Single = 0.3 'The height of each rectangle
Const sngShapeWidth As Single = 1.2 'The width of each rectangle
Const lInputColor As Long = 255 'Red 'Color of Input blocks
Const lCentralColor As Long = 16711680 'Blue 'Color of Central block
Const lOutputColor As Long = 65280 'Green 'Color of Output blocks
'======================================================================================================
'======================================================================================================
Dim AppVisio As Object
Dim lLastArrayRow As Long
Dim vArray As Variant
Dim bfound As Boolean
Dim lX As Long, lY As Long, lZ As Long
Dim sngPageWidth As Single
Dim sngPageHeight As Single
Dim vsoSelection As Visio.Selection
Dim lngShapeIDs() As Long
Dim lngShapeID As Long
Dim sngShapeHCenter As Single
Dim sngShapeVCenter As Single
Dim sngPlotCount As Single
Dim aryPlotted() As Variant
Dim lPlotRows As Long
Dim lMaxRows As Long
Dim vsoCell1 As Visio.Cell
Dim vsoCell2 As Visio.Cell
Dim lColor As Long
Const lOutputGridMaxHorizontalCount As Long = 2 'DON'T CHANGE THIS - CODE WILL BREAK = 2
lMaxRows = lOutputGridMaxHorizontalCount * lOutputGridMaxVerticalCount
lLastArrayRow = Cells(Rows.Count, 1).End(xlUp).Row
vArray = ActiveSheet.Range(Cells(2, 1), Cells(lLastArrayRow, 3))
ReDim Preserve aryPlotted(1 To 1)
aryPlotted(1) = "xyzzy"
lLastArrayRow = UBound(vArray, 1)
If lLastArrayRow > lMaxRows Then
MsgBox "Unable to plot more than " & lMaxRows & " rows of From/To data."
GoTo End_Sub
End If
If lLastArrayRow > lOutputGridMaxVerticalCount Then
lPlotRows = lOutputGridMaxVerticalCount
Else
lPlotRows = lLastArrayRow
End If
Set AppVisio = CreateObject("visio.application")
AppVisio.Visible = True
'Open new document
AppVisio.Documents.AddEx "", visMSDefault, 0
'Add required stencils
AppVisio.Documents.OpenEx "basflo_u.vss", visOpenRO + visOpenDocked
AppVisio.Documents.OpenEx "connec_u.vss", visOpenRO + visOpenDocked
If AppVisio.ActivePage.Shapes.Count > 0 Then
AppVisio.ActiveWindow.DeselectAll
AppVisio.ActiveWindow.SelectAll
End If
Set vsoSelection = AppVisio.ActiveWindow.Selection
Call vsoSelection.GetIDs(lngShapeIDs)
sngPageWidth = AppVisio.ActivePage.PageSheet.Cells("pagewidth")
sngPageHeight = AppVisio.ActivePage.PageSheet.Cells("pageHeight")
For lX = LBound(vArray, 1) To UBound(vArray, 1) '0 to X (number of input rows)
For lY = LBound(vArray, 2) To UBound(vArray, 2) '0 to 2
bfound = False
'Compare to shapes already present (not required for this app since new page each time)
For lngShapeID = LBound(lngShapeIDs) To UBound(lngShapeIDs)
If AppVisio.ActivePage.Shapes(lngShapeIDs(lngShapeID)).Name = vArray(lX, lY) Then
bfound = True
Exit For
End If
Next
If vArray(lX, lY) = vbNullString Then bfound = True 'Don't process blanks
'Compare to shapes just plotted
For lZ = LBound(aryPlotted) To UBound(aryPlotted)
If aryPlotted(lZ) = vArray(lX, lY) Then
bfound = True
Exit For
End If
Next
If Not bfound Then 'If not then add it
If lLastArrayRow / 2 <> Int(lLastArrayRow / 2) Then lLastArrayRow = lLastArrayRow + 1
sngPlotCount = sngPlotCount + 1
ReDim Preserve aryPlotted(1 To sngPlotCount)
aryPlotted(sngPlotCount) = vArray(lX, lY)
Select Case lY
Case 1
lColor = lCentralColor
sngShapeHCenter = sngPageWidth / 2 'Center
sngShapeVCenter = sngPageHeight / 2 'Center
Case 2
lColor = lInputColor
Select Case lLastArrayRow
Case Is <= lOutputGridMaxVerticalCount 'plot in 1 column
sngShapeHCenter = 1 * sngPageWidth / 4 'Left Column (From)
sngShapeVCenter = sngPageHeight * (lPlotRows + 1 - lX) / (lPlotRows + 1)
Case Else
If lX <= lOutputGridMaxVerticalCount Then 'plot in 2 columns
sngShapeHCenter = 1 * sngPageWidth / 6 'Left Column (From)
sngShapeVCenter = sngPageHeight * (lPlotRows + 1 - lX) / (lPlotRows + 1)
Else
sngShapeHCenter = 2 * sngPageWidth / 6 'Left Column (From)
sngShapeVCenter = sngPageHeight * (lPlotRows + lPlotRows + 1 - lX) / (lPlotRows + 1)
End If
End Select
Case 3
lColor = lOutputColor
Select Case lLastArrayRow
Case Is <= lOutputGridMaxVerticalCount
sngShapeHCenter = 3 * sngPageWidth / 4 'Right Column (To)
sngShapeVCenter = sngPageHeight * (lPlotRows + 1 - lX) / (lPlotRows + 1)
Case Else
If lX <= lOutputGridMaxVerticalCount Then
sngShapeHCenter = 4 * sngPageWidth / 6 'Right Column (To)
sngShapeVCenter = sngPageHeight * (lPlotRows + 1 - lX) / (lPlotRows + 1)
Else
sngShapeHCenter = 5 * sngPageWidth / 6 'Right Column (To)
sngShapeVCenter = sngPageHeight * (lPlotRows + lPlotRows + 1 - lX) / (lPlotRows + 1)
End If
End Select
End Select
With AppVisio.ActiveWindow.Page
.Drop AppVisio.Documents.Item("BASFLO_U.VSS").Masters.ItemU("Process"), 2.25, 9.25
Set vsoSelection = AppVisio.ActiveWindow.Selection
Call vsoSelection.GetIDs(lngShapeIDs)
With .Shapes.ItemFromID(lngShapeIDs(0))
.CellsSRC(visSectionObject, visRowXFormOut, visXFormPinX).FormulaU = sngShapeHCenter
.CellsSRC(visSectionObject, visRowXFormOut, visXFormPinY).FormulaU = sngShapeVCenter
.CellsSRC(visSectionObject, visRowXFormOut, visXFormWidth).FormulaU = sngShapeWidth
.CellsSRC(visSectionObject, visRowXFormOut, visXFormHeight).FormulaU = sngShapeHeight
.Characters.Text = vArray(lX, lY)
.Name = vArray(lX, lY)
.CellsSRC(visSectionCharacter, 0, visCharacterColor).FormulaU = _
"THEMEGUARD(" & ConvertLongToRGBString(lColor) & ")"
.CellsSRC(visSectionObject, visRowLine, visLineColor).FormulaU = _
"THEMEGUARD(" & ConvertLongToRGBString(lColor) & ")"
End With
End With
End If
Next
Next
For lX = LBound(vArray, 1) To UBound(vArray, 1) '0 to X
If vArray(lX, 1) <> vbNullString And vArray(lX, 2) <> vbNullString Then
AppVisio.ActiveWindow.Page.Drop AppVisio.Documents.Item("CONNEC_U.VSS").Masters.ItemU("Line connector"), 5#, 5#
Set vsoSelection = AppVisio.ActiveWindow.Selection
Call vsoSelection.GetIDs(lngShapeIDs)
With AppVisio.ActiveWindow.Page.Shapes.ItemFromID(lngShapeIDs(0))
.Name = "C" & CStr(2 * lX)
End With
Set vsoCell1 = AppVisio.ActiveWindow.Page.Shapes.ItemFromID(lngShapeIDs(0)).CellsU("BeginX")
Set vsoCell2 = AppVisio.ActiveWindow.Page.Shapes(CStr(vArray(lX, 2))).CellsSRC(1, 1, 0)
vsoCell1.GlueTo vsoCell2
Set vsoCell1 = AppVisio.ActiveWindow.Page.Shapes.ItemFromID(lngShapeIDs(0)).CellsU("EndX")
Set vsoCell2 = AppVisio.ActiveWindow.Page.Shapes(CStr(vArray(lX, 1))).CellsSRC(1, 1, 0)
vsoCell1.GlueTo vsoCell2
With AppVisio.ActiveWindow.Page.Shapes.ItemFromID(lngShapeIDs(0))
.CellsSRC(visSectionObject, visRowLine, visLineEndArrow).FormulaU = "13"
.SendToBack
.CellsSRC(visSectionObject, visRowLine, visLineColor).FormulaU = _
"THEMEGUARD(" & ConvertLongToRGBString(lInputColor) & ")"
End With
End If
If vArray(lX, 1) <> vbNullString And vArray(lX, 3) <> vbNullString Then
AppVisio.ActiveWindow.Page.Drop AppVisio.Documents.Item("CONNEC_U.VSS").Masters.ItemU("Line connector"), 4#, 5#
Set vsoSelection = AppVisio.ActiveWindow.Selection
Call vsoSelection.GetIDs(lngShapeIDs)
With AppVisio.ActiveWindow.Page.Shapes.ItemFromID(lngShapeIDs(0))
.Name = "C" & CStr(2 * lX) + 1
End With
Set vsoCell1 = AppVisio.ActiveWindow.Page.Shapes.ItemFromID(lngShapeIDs(0)).CellsU("BeginX")
Set vsoCell2 = AppVisio.ActiveWindow.Page.Shapes(CStr(vArray(lX, 1))).CellsSRC(1, 1, 0)
vsoCell1.GlueTo vsoCell2
Set vsoCell1 = AppVisio.ActiveWindow.Page.Shapes.ItemFromID(lngShapeIDs(0)).CellsU("EndX")
Set vsoCell2 = AppVisio.ActiveWindow.Page.Shapes(CStr(vArray(lX, 3))).CellsSRC(1, 1, 0)
vsoCell1.GlueTo vsoCell2
With AppVisio.ActiveWindow.Page.Shapes.ItemFromID(lngShapeIDs(0))
.CellsSRC(visSectionObject, visRowLine, visLineEndArrow).FormulaU = "13"
.SendToBack
.CellsSRC(visSectionObject, visRowLine, visLineColor).FormulaU = _
"THEMEGUARD(" & ConvertLongToRGBString(lOutputColor) & ")"
End With
End If
Next
AppVisio.ActiveWindow.DeselectAll
End_Sub:
Set vsoCell1 = Nothing
Set vsoCell2 = Nothing
Set vsoSelection = Nothing
Set AppVisio = Nothing
End Sub
Function ConvertLongToRGBString(lRGB As Long) As String
Dim bR As Byte, bG As Byte, bB As Variant
bR = lRGB Mod 256
bG = Int(lRGB / 256) Mod 256
bB = Int(lRGB / 256 / 256) Mod 256
ConvertLongToRGBString = "RGB(" & CStr(bR) & "," & CStr(bG) & "," & CStr(bB) & ")"
End Function