Shapes Not Moving With Container Moved by VBA Code

Started by PAHTDC, October 24, 2015, 04:24:24 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

PAHTDC

Just getting started with Visio, and everything I have read to-date suggests this shouldn't happen (ha ha - programmers everywhere), but struggling with following:

I add a container shape to a page, then add some existing shapes to it:


[snip]
containerToAdd = "Plain"
Set ContainerShape = Visio.ActivePage.DropContainer(Visio.ActiveDocument.Masters.ItemU(containerToAdd), shpUnqItm)
'       ContainerShape.ContainerProperties.ResizeAsNeeded = visContainerAutoResizeExpandContract

' Not shown code here iterates through existing shapes on the page to get an ID so can grab and add shapes to the new container.

Set shpSub = Visio.ActivePage.Shapes.ItemFromID(arySubShapeIDs(iSubShape))
ContainerShape.ContainerProperties.AddMember shpSub, visMemberAddExpandContainer


The part that I don't understand is that when I subsequently try to move the container (ContainerShape), the shapes that have been added don't move with it (sometimes). 


' This code moves the container, but not the shapes in it...
ContainerShape.SetCenter 100, -1 * ContainerShape.Cells("Height").ResultIU / 2


I say sometimes as at various points over last 24 hours the shapes have moved with the container, but i can't figure out what i'm changing elsewhere in my code that is affecting this behavior.

QUESTION - Are there any known 'gotchas' that I could be un-intentionally doing that would produce this effect?

I have some code grabbed from elsewhere that was trying to optimise drawing performance, there sort-of sometimes seemed to be a correlation between turning these items on/off that affected the behavior (at some points the shapes were moving with the container, but I can't reproduce changes here affecting the behavior).  Could AutoLayout be affecting this behavior?


'    Visio.Application.DeferRecalc = 1
'    Visio.Application.EventsEnabled = False
'    Visio.Application.ScreenUpdating = False
'   Visio.Application.AutoLayout = False
'   Visio.Application.InhibitSelectChange = True
'   Visio.Application.ShowChanges = False

' [Do drawing processing]

'Some reset stuff here:
    Visio.Application.DeferRecalc = 0
    Visio.Application.EventsEnabled = True
    Visio.Application.ScreenUpdating = True
    Visio.Application.AutoLayout = True
    Visio.Application.InhibitSelectChange = False
    Visio.Application.ShowChanges = True


Thanks for any clues / help.....

PAHTDC

Paul Herber

I don't know this "SetCenter" method, but try setting the container shape's PinX and PinY values instead.
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

PAHTDC

Quote from: Paul Herber on October 24, 2015, 11:43:09 PM
I don't know this "SetCenter" method, but try setting the container shape's PinX and PinY values instead.

Thanks for the suggestion Paul - unfortunately I'm getting the exact same effect using PinX and PinY.


ContainerShape.Cells("pinX").ResultIU = 100
ContainerShape.Cells("pinY").ResultIU = -1 * ContainerShape.Cells("Height").ResultIU / 2


The part I find very confusing is that if I put a breakpoint in the code on the PinX line and go to the Visio window and grab the container with the mouse, the shapes will move around with it.  But using PinX/PinY or the SetCenter methods both just move the container and leave the shapes behind (appear to remove them from the container).

PAHTDC

PAHTDC

For the benefit of anyone else who runs into this issue, I believe I have identified the culprit.

Somehow/where the property ActiveDocument.DiagramServicesEnabled got set to 0 (as seen in a Watch).  Despite my never setting it to that anywhere in code.

From https://msdn.microsoft.com/en-us/library/office/ff765437.aspx, this means no structured diagram services, which means no containers which means..... no shapes moving when the container moves.

Setting
ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150
seems to have fixed my issue.

Also making DeferRecalc = 1 seems like a bad idea - has been producing strange effects where code runs differently depending on whether a breakpoint is enabled or not.

PAHTDC

Paul Herber

The code:


'    Visio.Application.DeferRecalc = 1
'    Visio.Application.EventsEnabled = False
'    Visio.Application.ScreenUpdating = False
'   Visio.Application.AutoLayout = False
'   Visio.Application.InhibitSelectChange = True
'   Visio.Application.ShowChanges = False


will certainly stop many things from working properly.

Create a macro and look at the code it generates to see the basics of what is needed.
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/