Recently I have been working on some Visio shapes that were basically a stack of rectangles. Though Visio has tools for aligning and distributing shapes, it does not have an easy way of stacking shapes. If you are lucky enough to have shapes that are multiples of the grid size, it is a piece of cake. If not, the guides can be used to connect a shape, above or below the guide. If you want to do more than two it gets tricky. First, you have to determine the top of the shape to determine where to place the next guide. Then use the Size / Position dialog to set the position of the guide. You can then add the next shape to the guide.
This of course would be tedious, but long before Visio was acquired by Microsoft, it was the first non Microsoft company to fully implement VBA. So, now the problem is trivial, turn on developer mode and write some VBA. So, this routine will take the selected shapes and stack them.
Public Sub StackVertical()
Dim vsoSelect As Visio.Selection
Dim vsoShape As Visio.Shape
Dim ShapeH As Double
Dim OldShapeH As Double
Dim pin As Double
Set vsoSelect = Visio.ActiveWindow.Selection
If vsoSelect.Count > 0 Then
Set vsoShape = ActiveWindow.Selection.Item(1)
pin = vsoShape.Cells(“piny”).Result(“inches”) + vsoShape.Cells(“LocPinY”).Result(“inches”)
For Each vsoShape In vsoSelect
ShapeH = vsoShape.Cells(“Height”).Result(“inches”)
vsoShape.Cells(“piny”) = pin – (OldShapeH + ShapeH) / 2
pin = pin – (OldShapeH + ShapeH) / 2
OldShapeH = ShapeH
Next vsoShape
Else
MsgBox “You Must Have Something Selected”
End If
End Sub
… and the following code will stack the shapes horizontally.
Public Sub StackHorizontal()
Dim vsoSelect As Visio.Selection
Dim vsoShape As Visio.Shape
Dim ShapeW As Double
Dim OldShapeW As Double
Dim pin As Double
Set vsoSelect = Visio.ActiveWindow.Selection
If vsoSelect.Count > 0 Then
Set vsoShape = ActiveWindow.Selection.Item(1)
pin = vsoShape.Cells(“pinx”).Result(“inches”) – vsoShape.Cells(“LocPinx”).Result(“inches”)
For Each vsoShape In vsoSelect
ShapeW = vsoShape.Cells(“Width”).Result(“inches”)
vsoShape.Cells(“pinx”) = pin + (OldShapeW + ShapeW) / 2
pin = pin + (OldShapeW + ShapeW) / 2
OldShapeW = ShapeW
Next vsoShape
Else
MsgBox “You Must Have Something Selected”
End If
End Sub
Though you could do a select area, it may be safer to use the Ctrl key to select the shapes in the stacking order you want.
Enjoy.
John Marshall… Visio MVP Visio.MVPs.org