Container behavior different between user and VBA shape moves

Started by Visisthebest, March 04, 2021, 05:17:01 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Visisthebest

I have a container that is set to locked and always fits to contents.

The user moves the shapes in the container by hand, the container keeps containing the shapes.

I move the shapes with VBA code (only changing the shape's PinX & PinY position) and the container loses the shapes.

Is there a way to get the VBA to act identical to a user moving a shape? For the diagram layout (500+ shapes) using select and move selection would be insanely slow unfortunately so looking for another way.

Visio 2021 Professional

Thomas Winkel

#1
Hi,

the macro recorder selects the container in the active page and moves the selection in the active window.
I think that is equivalent to user drag & drop action.
But this will only work on the active page.

I use the select method that you describe:

Sub MoveContainer(container As Visio.Shape, dx As Double, dy As Double)
    Dim memberID As Variant
    Dim shp As Visio.Shape
    Dim sel As Visio.Selection
   
    Set sel = container.ContainingPage.CreateSelection(visSelTypeEmpty)
   
    sel.Select container, VisSelectArgs.visSelect
   
    For Each memberID In container.ContainerProperties.GetMemberShapes(visContainerFlagsDefault)
        Set shp = ActivePage.Shapes.ItemFromID(memberID)
        sel.Select shp, VisSelectArgs.visSelect
    Next
   
    sel.Move dx, dy
End Sub


I never made speed tests, but we don't have so many shapes in a container.

Visisthebest

Thank you Thomas this is a solution that would work, have to think about how it lines up with the exact shape moves so it fits the entire diagram layout.
Visio 2021 Professional

Thomas Winkel

I guess I misunderstood your first post due to my poor English...

Maybe this is what you need:

Sub MoveShapeInContainer(shp As Visio.Shape, xPos As Double, yPos As Double)
    Dim container As Visio.Shape
    Dim memberId As Variant
   
    'Assume that the shape is member of max 1 container
    For Each memberId In shp.MemberOfContainers
        Set container = shp.ContainingPage.Shapes.ItemFromID(memberId)
    Next memberId
   
    shp.Cells("PinX") = xPos
    shp.Cells("PinY") = yPos
   
    If container Is Nothing Then Exit Sub
   
    container.ContainerProperties.LockMembership = False
    container.ContainerProperties.AddMember shp, visMemberAddExpandContainer
    container.ContainerProperties.LockMembership = True
End Sub

Visisthebest

Thank you Thomas that solution will work, moving the shape then putting it back in the container. Thank you for your help!
Visio 2021 Professional