Visio Guy

Visio Discussions => Programming & Code => Topic started by: perry59 on November 22, 2020, 11:17:08 PM

Title: deleting shapes from container
Post by: perry59 on November 22, 2020, 11:17:08 PM
I need to be able to Programmatically delete shapes from a container.
attempting to do so manually results in a protection error, so I assume I'll get the same through code.
can anyone tell me how to accomplish this?
thanks!
Title: Re: deleting shapes from container
Post by: OldSchool1948 on November 23, 2020, 11:33:05 PM
Try this

Private Sub deleteContainerShapes()

    Dim vsoPage As Visio.Page
    Set vsoPage = Visio.ActivePage
   
    Dim vsoSelection As Visio.Selection
    Set vsoSelection = Visio.ActiveWindow.Selection
   
    If vsoSelection.Count = 0 Then
        MsgBox "Nothing Selected"
        Exit Sub
    End If
   
    Dim i As Integer
    For i = 1 To vsoSelection.Count
   
        Dim vsoContainer As Visio.Shape
        Set vsoContainer = vsoSelection(i)
       
        '// Unlock the group
        vsoContainer.Cells("LockGroup").formula = "=0"
   
        '// Get the Shape IDs of all Container Shapes
        Dim ShapeIDs() As Long
        ShapeIDs = vsoContainer.ContainerProperties.GetMemberShapes(visContainerFlagsDefault)
                   
        Dim s As Integer
        For s = LBound(ShapeIDs) To UBound(ShapeIDs)
           
            '// Get each container shape
            Dim vsoContainerShp As Visio.Shape
            Set vsoContainerShp = vsoPage.Shapes.ItemFromID(ShapeIDs(s))
           
            '// Remove Delete Protection
            vsoContainerShp.Cells("LockDelete").formula = "=0"
            vsoContainerShp.Delete
           
        Next s
       
    Next i
   
End Sub

Title: Re: deleting shapes from container
Post by: perry59 on November 24, 2020, 08:29:48 PM
I didn't even realize that dropping shapes into a container made them part of a "group".
all I had to do was clear the protection on the container, delete the shape that was in it, then put the protection back.
easy peasy!
thanks
Title: Re: deleting shapes from container
Post by: perry59 on November 25, 2020, 12:00:03 AM
Sorry, but it seems this was all for naught.
I am new to "containers"
you don't need to do anything special to delete a shape from a container, just delete it.
my problem was I was dropping shapes onto a container (which contained a group to represent a D-sub connector) those shapes were becoming part of the GROUP, not members of the container.
I was not calling ContainerProperties.AddMember. so later if one or more of those shapes needed to be deleted, nothing showed up in the call to ContainerProperties.GetMemberShapes

I need to restructure my code :(