Author Topic: How to Delete All But a Specific Set of Shapes?  (Read 12019 times)

0 Members and 1 Guest are viewing this topic.

Aranel

  • Newbie
  • *
  • Posts: 7
How to Delete All But a Specific Set of Shapes?
« on: July 06, 2015, 01:41:18 PM »
 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:

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
        vsoDataRecordset.Delete
    Next
   
   
    Call Drawflowpath(ComboBox21.Text)
   
     ActiveWindow.DeselectAll
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   
                    shp.Delete                                           
                 End If                                                       
            Else                                                               
                shp.Delete                                               
            End If                                                           
        Next                                                                 
End Sub                                         


Any help is much appreciated!
« Last Edit: July 07, 2015, 09:11:00 AM by Aranel »

JohnGoldsmith

  • Sr. Member
  • ****
  • Posts: 358
    • John Goldsmith's visLog
Re: How to Delete All But a Specific Set of Shapes?
« Reply #1 on: July 06, 2015, 04:16:50 PM »
Hi,

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: http://visualsignals.typepad.co.uk/vislog/2007/11/looping-through.html

Hope that helps.

Best regards

John
« Last Edit: July 07, 2015, 02:29:53 AM by Visio Guy »
John Goldsmith - Visio MVP
http://visualsignals.typepad.co.uk/

Visio Guy

  • Administrator
  • Hero Member
  • *****
  • Posts: 1737
  • Smart Graphics for Visual People...n' Stuff
    • Visio Guy
Re: How to Delete All But a Specific Set of Shapes?
« Reply #2 on: July 06, 2015, 04:41:53 PM »
Or build a Visio.Selection of the shapes to be deleted, then just call visSel.Delete when you're done looping.
« Last Edit: July 07, 2015, 02:29:58 AM by Visio Guy »
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

Visio Guy

  • Administrator
  • Hero Member
  • *****
  • Posts: 1737
  • Smart Graphics for Visual People...n' Stuff
    • Visio Guy
Re: How to Delete All But a Specific Set of Shapes?
« Reply #3 on: July 07, 2015, 02:29:12 AM »
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.

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( _
        Visio.VisSelectionTypes.visSelTypeEmpty)
       
  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
                                                                           
  Next
 
  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 http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

Visio Guy

  • Administrator
  • Hero Member
  • *****
  • Posts: 1737
  • Smart Graphics for Visual People...n' Stuff
    • Visio Guy
Re: How to Delete All But a Specific Set of Shapes?
« Reply #4 on: July 07, 2015, 05:30:31 AM »
Ok, this thread has now benn ARTICLE-IFIED!

Deleting Visio Shapes Programmatically
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

Aranel

  • Newbie
  • *
  • Posts: 7
Re: How to Delete All But a Specific Set of Shapes?
« Reply #5 on: July 07, 2015, 07:41:00 AM »
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!

damswil

  • Jr. Member
  • **
  • Posts: 20
Re: How to Delete All But a Specific Set of Shapes?
« Reply #6 on: July 10, 2015, 02:52:26 PM »
Does anyone know how I could do this across multiple pages in a single document?

Visio Guy

  • Administrator
  • Hero Member
  • *****
  • Posts: 1737
  • Smart Graphics for Visual People...n' Stuff
    • Visio Guy
Re: How to Delete All But a Specific Set of Shapes?
« Reply #7 on: July 11, 2015, 05:56:51 AM »
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 http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

dkara

  • Newbie
  • *
  • Posts: 2
Re: How to Delete All But a Specific Set of Shapes?
« Reply #8 on: January 23, 2017, 01:59:19 PM »
Hey!

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 :(

« Last Edit: January 24, 2017, 08:57:58 AM by Paul Herber »

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4917
  • Ideas Visio-lized into solutions
Re: How to Delete All But a Specific Set of Shapes?
« Reply #9 on: January 23, 2017, 08:49:48 PM »
What's the other program?

Wapperdude
Visio 2019 Pro

dkara

  • Newbie
  • *
  • Posts: 2
Re: How to Delete All But a Specific Set of Shapes?
« Reply #10 on: January 24, 2017, 03:07:07 AM »
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.
« Last Edit: January 24, 2017, 08:58:22 AM by Paul Herber »

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4917
  • Ideas Visio-lized into solutions
Re: How to Delete All But a Specific Set of Shapes?
« Reply #11 on: January 24, 2017, 12:01:49 PM »
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.

Wapperdude

« Last Edit: January 25, 2017, 10:28:15 AM by wapperdude »
Visio 2019 Pro