News:

BB code in posts seems to be working again!
I haven't turned on every single tag, so please let me know if there are any that are used/needed but not activated.

Main Menu

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.

Aranel

 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
        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!

JohnGoldsmith

#1
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
John Goldsmith - Visio MVP
http://visualsignals.typepad.co.uk/

Visio Guy

#2
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 http://www.visguy.com
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( _
        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

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

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

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 http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

dkara

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


wapperdude

Visio 2019 Pro

dkara

#10
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.

wapperdude

#11
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

Visio 2019 Pro

Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 360 (show)
Files included: 34 - 1306KB. (show)
Memory used: 1255KB.
Tokens: post-login.
Cache hits: 16: 0.00253s for 26,724 bytes (show)
Cache misses: 6: (show)
Queries used: 16.

[Show Queries]