Visio Guy

Visio Discussions => Visio Bugs (er...Issues) => Topic started by: Hey Ken on December 31, 2015, 02:10:02 PM

Title: ActivePage.Connects.Count is not being updated
Post by: Hey Ken on December 31, 2015, 02:10:02 PM
   Okay, here we go again.  One more Visio conundrum to ring out the old year...

   In Visio 2013 professional, I have two simple shapes: a box and a line.  The box has a Type 0 connection point at the middle of the bottom, and the line has a Type 1 point slightly off the line at its middle.  Using VBA and starting with an otherwise-empty page, I connect the middle point of the line to the bottom point of the box as follows:


TheLine.Cells("BeginX").Formula = "=Guard(PNTX(LOCTOPAR(PNT(" & TheBox.Name & "!Connections.X1," & TheBox.Name & "!Connections.Y1)," & TheBox.Name & "!EventXFMod,EventXFMod))+0.25 in)"
TheLine.Cells("BeginY").Formula = "=Guard(PNTY(LOCTOPAR(PNT(" & TheBox.Name & "!Connections.X1," & TheBox.Name & "!Connections.Y1)," & TheBox.Name & "!EventXFMod,EventXFMod))-0.05 in)"
TheLine.Cells("EndX").Formula = "=Guard(PNTX(LOCTOPAR(PNT(" & TheBox.Name & "!Connections.X1," & TheBox.Name & "!Connections.Y1)," & TheBox.Name & "!EventXFMod,EventXFMod))-0.25 in)"
TheLine.Cells("EndY").Formula = "=Guard(PNTY(LOCTOPAR(PNT(" & TheBox.Name & "!Connections.X1," & TheBox.Name & "!Connections.Y1)," & TheBox.Name & "!EventXFMod,EventXFMod))-0.05 in)"
TheLine.Cells("Width").Formula = "=Guard(" & TheBox.Name & "!Width)"

Debug.Print ActivePage.Connects.Count

   The net result is that I have a box with a line connected under it, sort of an underlined box, and when you move the box around, the two shapes travel together like any decent connected shapes should.   But... after I do the same connection, VBA reports the ActivePage.Connects.Count is zero.  If I do the connection manually, it’s 2.  Why the zero??

   FYI, I chose those formulas because that’s what Visio does when I connect them manually; the VBA vs. manual connection formulas are identical. I added the GUARD myself, but removing it had no effect on Connects.Count.

   Any thoughts?
Title: Re: ActivePage.Connects.Count is not being updated
Post by: wapperdude on December 31, 2015, 07:21:52 PM
It wasn't clear to me that your code actually glues the shapes.  So, I did a little experiment and created a box with a connection point and a line with a connection point as you describe.  Then, manually glued the line to the box, shuffled the box around, and his new buddy the line followed him like a good little puppy dog.

Then, I disconnected the two, deselected, and then started the macro recorder.  There is a difference.  And, the connection count does update.  Note, this is for V2007, but would anticipate V2013 to be the same in this regard.

Happy New Year,

Sub Macro1()

    Dim vsoCell1 As Visio.Cell
    Dim vsoCell2 As Visio.Cell

    MsgBox ActivePage.Connects.Count
    ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemFromID(2), visSelect
    Application.ActiveWindow.Selection.Move 0#, 0.875
    Set vsoCell1 = Application.ActiveWindow.Page.Shapes.ItemFromID(2).CellsU("BeginX")
    Set vsoCell2 = Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(7, 0, 0)

    vsoCell1.GlueTo vsoCell2                  'Hey! this is missing in Ken's code.

    Set vsoCell1 = Application.ActiveWindow.Page.Shapes.ItemFromID(2).CellsU("EndX")
    Set vsoCell2 = Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(7, 0, 0)
    vsoCell1.GlueTo vsoCell2

    MsgBox ActivePage.Connects.Count
End Sub
Title: Re: ActivePage.Connects.Count is not being updated
Post by: Hey Ken on January 03, 2016, 01:26:45 PM


   Thanks for taking a look.  Turns out V2013 and my old V2003 behave the same way as your V2007, so it had to be something deeper.  And based on your input, I was able to find out exactly what it was, and subsequently achieve my goal of a Visio-connected "underlined box".

   What my original code did was to marry the box and line together irrespective of any connection, and the two shapes just traveled together according to the line's endpoint formulae.  Without your suggested GlueTo, there was no connection in the Visio sense, and therefore no Connects.Count. 

   Taking it a step further, the final code to connect the two turned out to be even simpler than your approach...


Set TheLineConnectionsX1 = TheLine.Cells("Connections.X1")
Set TheBoxConnectionsX1 = TheBox.Cells("Connections.X1")
TheLineConnectionsX1.GlueTo TheBoxConnectionsX1

   ...which glues the two connection points to each other in the Visio sense, yielding a Connects.Count of 2, as expected, and an underlined box, as desired.

   Thanks again for putting me on the path to solving my problem.  You're already making it a Happy New Year, even though it's only a few dozen hours old.

   - Ken