How to Delete All But a Specific Set of Shapes?

Started by Aranel, July 06, 2015, 06:41:18 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


 I'm pretty far into a project to create flow charts programmatically. I was previously able to delete and redraw the charts, but now that I've added a drop down menu it no longer works.

Here's the code:

Private Sub Document_RunModeEntered(ByVal doc As IVDocument)
    Call m_updatePageList
End Sub

Private Sub ComboBox21_Change()

    Call DeleteShapes     
     Dim vsoDataRecordset As Visio.DataRecordset
    For Each vsoDataRecordset In ActiveDocument.DataRecordsets
    Call Drawflowpath(ComboBox21.Text)
End Sub

Private Sub DeleteShapes()           
    Dim shp As Visio.Shape                                       
   Dim vsoLayer As Visio.Layer                                 
        For Each shp In ActivePage.Shapes                 
            If (shp.LayerCount > 0) Then                       
                Set vsoLayer = shp.Layer(1)                   
                If (vsoLayer.Name <> "Buttons") Then   
                 End If                                                       
            End If                                                           
End Sub                                         

Any help is much appreciated!



You need to use a for loop (rather than for each) and step backwards to avoid stepping over the upper index.

Have a look at the 'Delete all decision shapes' example here:

Hope that helps.

Best regards

John Goldsmith - Visio MVP

Visio Guy

Or build a Visio.Selection of the shapes to be deleted, then just call visSel.Delete when you're done looping.
For articles, tips and free content, see the Visio Guy Website at
Get my Visio Book! Using Microsoft Visio 2010

Visio Guy

Here's some clean, refactored VBA to do what you want to do. TestIt shows how you would call m_deleteShapes from your main code.

Public Sub TestIt()
  Call m_deleteShapes(Visio.ActivePage)
End Sub

Private Sub m_deleteShapes(ByRef visPg As Visio.Page)
  Dim selToDel As Visio.Selection
  Set selToDel = visPg.CreateSelection( _
  Dim shp As Visio.Shape
  For Each shp In visPg.Shapes
    '// Add shape to selection if it is NOT on the
    '// Buttons layer:
    If (m_isShapeOnButtonsLayer(shp) = False) Then
      Call selToDel.Select(shp, Visio.VisSelectArgs.visSelect)
    End If
  If (selToDel.Count > 0) Then
    Call selToDel.Delete
  End If
  '// Cleanup:
  Set selToDel = Nothing
  Set shp = Nothing
End Sub

Private Function m_isShapeOnButtonsLayer( _
      ByRef visShp As Visio.Shape) As Boolean
  '// Start pessimistically:
  m_isShapeOnButtonsLayer = False
  '// Shapes can belong to more than one layer.
  '// Check all layers that the shape might belong to:
  Dim lyr As Visio.Layer
  Dim i As Integer
  For i = 1 To visShp.LayerCount
    '// Get the ith layer for the shape:
    Set lyr = visShp.Layer(i)
    '// Compare the layer name to "Buttons":
    If (StrComp(lyr.Name, "Buttons", vbTextCompare) = 0) Then
      m_isShapeOnButtonsLayer = True
      Exit For
    End If
  Next i
  '// Cleanup:
  Set lyr = Nothing

End Function

For articles, tips and free content, see the Visio Guy Website at
Get my Visio Book! Using Microsoft Visio 2010

Visio Guy

For articles, tips and free content, see the Visio Guy Website at
Get my Visio Book! Using Microsoft Visio 2010


Thanks for all the help!

I thought of using a selection, but I thought Selection.Delete deleted the Selection, not the shapes in the selection. I figured that's what DeleteEx did, which I don't have in my version of Visio (2007).

Ok, I'm off to try out all your suggestions!

Thanks again!


Does anyone know how I could do this across multiple pages in a single document?

Visio Guy

Just repeat for each page in the document, ala: foreach(Vis.Page pg in doc.Pages){...}

Visio.Selection objects can only apply to a page, so there would be no way to make one call to Visio that deleted shapes on multiple pages anyway, if that makes any sense.
For articles, tips and free content, see the Visio Guy Website at
Get my Visio Book! Using Microsoft Visio 2010



I need help on this topic, it will be a more for dummy type though.

I have been using another program to design process flows, but to share, I converted it into a Visio file but with a problem. It automatically adds a box on a connector. If I can delete all the empty boxes (others have texts) my problem will be solved.

I am not familiar with Visio, and I don't want to spend hours to select all the boxes one by one and delete...

Appreciate if you can help.

I checked all the topics related to "delete" but still couldn't figure it out :(


Visio 2019 Pro


It's developed for internal-use purposes, so not a common used one and,  I couldn't reach the developer.

Here is the attachment too, just want to delete boxes on connectors. can do it based on a color or size or pick the empty shapes. I just couldnt figure how to define the shape to be deleted.


Some thoughts...
  1)  you need either a unique property or combination of properties...I suspect the latter.
  2)  questions
         a)  are the sizes (width,length) always the same
         b)  color always the same
         c)  are they grouped or glued to the connector

So, you search all shapes on the page, or possibly just shapes attached to connectors, or all shapes that are not 1D.  Then, check each shape to see if there's no text, it's the right color, and the right size.  That should be a unique filter set.


Visio 2019 Pro