Select shapes based on fill color...then do something

Started by visioVBArookie, August 25, 2018, 10:49:05 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

visioVBArookie

I was able to figure out the answer to my last question once I realized the center of a shape is used to move it. For reference here is the final product.


Sub visioVBArookie()
' special thanks to Surrogate from visguy.com for writing the majority of this code

    Dim sh, shA, shB As Shape
    Dim shAwdth, shBwdth As Visio.Cell
    Dim shBwdth2 As Integer

' use current selection
    Set sh = ActiveWindow.Selection(1)

' check is first sub-shape of first sub-shape have RGB sub-string in formula in FillForegnd cell
    If InStr(sh.Shapes(1).Shapes(1).Cells("FillForegnd").Formula, "RGB") Then
        Set shA = sh.Shapes(1)
        Set shB = sh.Shapes(2)
    Else
        Set shA = sh.Shapes(2)
        Set shB = sh.Shapes(1)
    End If

' top shape alignment with shA as the anchor
    ActiveWindow.Select shA, visSubSelect
    ActiveWindow.Select shB, visSubSelect
    Application.ActiveWindow.Selection.Align visHorzAlignNone, visVertAlignTop, False
    ActiveWindow.DeselectAll
    ActiveWindow.Select sh, visSelect
    sh.UpdateAlignmentBox

' the following move uses the center of the shapes
' get width of shA and shB
    Set shAwdth = shA.Cells("width")
    Set shBwdth = shB.Cells("width")

' the next line of code is more or less processed in this manor:
' 1) using shA as the anchor, align center of shB to left edge of shA; '"Sheet." & shA.ID & "!PinX
' 2) get half of shB width and add to get left edge of shA aligned with left edge of shB; ' +" & "(" & shBwdth / 2 & ")
' 3) add width of shA to get shapes side-by-side; +" & shAwdth
' 4) apply 2 mm padding between shapes; + 2 mm
    shB.Cells("PinX").Formula = "Sheet." & shA.ID & "!PinX+ 2 mm+" & "(" & shBwdth / 2 & ")+" & shAwdth
    sh.UpdateAlignmentBox
    ActiveWindow.DeselectAll

End Sub

wapperdude

The location of the "reference" is determined by the LocPinX and LocPinY cells.  By default, it is the center of the shape, i.e., width*0.5, and height*0.5, respectively.

Wapperdude
Visio 2019 Pro