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

Delete a shapee(s) in VBA macro. Deleting all Cats is VBA

Started by DinoCobolMan, August 03, 2014, 06:18:05 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

DinoCobolMan

I would like to know how to delete a shapes in a Tic-Tac-Toe grid. I am using Tic-Tact-Toe as a simple example.

I AM NOT trying to play Tic-Tac-Toe with VBA.

I have three books on VBA for Visio and the SDK too, but none of the books show a Viso flowchart example using VBA code for the flowchart example.   

The the Tic-Tac-Toe grid is grouped together and does not need to be referenced to the delete shapes.
I have three circles on the grid names Cat, Dog and Fox. 

According to Drawing Explored the the names are:
   Cat, Cat.284, Cat.291
  Dog,  Dog.281, Dog.288
  Fox.280, Fox.270, Fox.274

Could someone PLEASE show me some sample code to delete all the Cats from my grid? 



Yacine

I understand you don't know where to start.
In your case you want to select all the shapes belonging to a same master - "cat".
You can get them by iterating through the activepage.shapes collection and checking for "shp.master.name".
Usually a simple shp.delete, would then be enough, but this method changes the shapes collection, causing the routine to skip some cats.
So you need to collect the hits somewhere. I chose to put them in the "activewindow.selection", which I deleted after having left the loop.

Here's the code:
Sub DeleteCats()

ActiveWindow.DeselectAll

For Each shp In ActivePage.Shapes
    If shp.Master.Name = "Cat" Then
        ActiveWindow.Select shp, visSelect
    End If
Next shp

ActiveWindow.Selection.Delete
End Sub
Yacine

DinoCobolMan

Thanks for your very helpful and quick response.  Does shp need to be defined with a DIM statement?
I copied your code and ran the macro, but I received this error.

Object variable not set (Error 91)

    If shp.Master.Name = "Cat" Then        <<- Error line
        ActiveWindow.Select shp, visSelect
    End If


Yacine

If your module has the statement "option explicit", then yes you need to declare "shp" (dim shp as shape).
Otherwise it should run as is.
Yacine

DinoCobolMan

Yacine,

I copied the code from the Visio Guy page to the Macro editor window.  I do not have Object Explicit coded.
Attached is the code and error being displayed.

Sub DeleteCats()

ActiveWindow.DeselectAll

For Each shp In ActivePage.Shapes
    If shp.Master.Name = "Cat" Then
        ActiveWindow.Select shp, visSelect
    End If
Next shp

ActiveWindow.Selection.Delete
End Sub

Thanks 

wapperdude

The code blows up because your drawing probably has shapes with no masters.  Modify as follows:


Sub DeleteCats()

ActiveWindow.DeselectAll

For Each shp In ActivePage.Shapes
    If Not (shp.Master Is Nothing) Then
        'MsgBox shp.Master
        If shp.Master.Name = "Cat" Then
            ActiveWindow.Select shp, visSelect
        End If
    End If
Next shp
ActiveWindow.Selection.Delete
End Sub


It should run now.
Wapperdude
Visio 2019 Pro

DinoCobolMan

Thank you Wapperdude and Yacine,

None of the the code has worked for me. 
I can write VBA macros Excel, but I can not get a handle on VBA for Visio. 

I truly appreciate your time and effort. 


wapperdude

Can you upload a Visio file, doesn't have to have everything.  Something with a cat, and the code.

Wapperdude
Visio 2019 Pro

DinoCobolMan

Wapperdude,

The code is exactly, what was posted.  I ran the code you sent me and I didn't get any kind of error, but nothing was deleted.
I attached the drawing, with code pasted into the the drawing, since I could not tell where the Macro are stored in Visio.
I just created a DeleteCats macro and ran DeleteCats form the macro screen.

Thanks,
DinoCobolMan

wapperdude

Looked at the file...there are numerous "issues".

1.)  The code belongs in the Visual Basic Editor.  Hit =<alt> + F11.  Double click ThisDocument under the Visio Objects.  That opens a window.  Paste the code there.

2.)  Your shapes are grouped.  The code isn't structured to look inside a group.  It could be modified, not a big deal.  For now, to demonstrate that the code works, just ungroup it.

3.)  Your Fox, Cat, Dog are not master shapes.  A master shape is called that because it is dragged from a stencil.  So, you need to open a new stencil
     a.  File > Shapes & scroll to the bottom.
     b.  Drag one of each shape onto the stencil.  Rename each to Fox, Cat, Dog.
     c.  Delete the shapes on the page, and drag new ones from the stencil

That should fix the various problems.  To access the menu, (I'm using V2007), menu bar>Tools>ThisDocument>DeleteCats

I've updated your file with a new page, and a new stencil.  The macro ran fine.

Wapperdude
Visio 2019 Pro

DinoCobolMan

Wapperdude,
Thanks! I will try what you told me to do when I get home.

wapperdude

#11
I was curious about Yacine's comment about the Master.Name method changing the collection.  Apparently, the shape index gets messed up as you progress thru the deleting process.  At least that's what I interpret from John Goldsmith's comment on deleting shapes by master.  His solution is to start at the highest index for the shapes collection and to work backwards to 1.  The adapted code is:
Sub DeleteCats()
Dim shp As Visio.Shape
Dim i As Integer

ActiveWindow.DeselectAll
For i = ActivePage.Shapes.Count To 1 Step -1
    Set shp = ActivePage.Shapes.ItemU(i)
    If Not (shp.Master Is Nothing) Then
        If shp.Master.Name = "Cat" Then
            shp.Delete
        End If
    End If
Next i
End Sub


Link to John's code:  http://visualsignals.typepad.co.uk/vislog/2007/11/looping-through.html

You know what they say, "Curiosity killed the cat, satisfaction brought it back".   ::)

Wapperdude
Visio 2019 Pro

Jumpy

I knew that I have to count backwards when looping through any collection or objectlists with For...Next and deleting sth. along the way. So as not to overstep the index boundaries. Another way, that only works if you delete every shape is to use a normal (Step+1) For...Next loop and always delete the first object or shape.

But what is new to me is, that the For Each...Next loop is affected by that. I thought the iterator of the For Each loop has some magic inside, that prevents that problem. There's always sth. new to learn it seems.

wapperdude

Yeah.  Well it never occurred to me that there would be an index problem at all.   :o
Visio 2019 Pro

DinoCobolMan

Thank you for all the responses!

I guess I don't really understand the Visio Object Model at this time. That is an issue along with confusing Excel macros and VBA in VISIO. Someone sent me an email about the two books below, and suggested these books would most explain the concepts for me.   If any of you have a suggestion on any other VBA for VISIO books or sites, please send me an email or post your suggestions on this page.  I will close out this request in a few days. 

Books.
Visualizing Information with Microsoft Office Visio
Microsoft Visio 2010 Business Process Diagramming and Validation

DinoCobolMan

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: 412 (show)
Files included: 34 - 1306KB. (show)
Memory used: 1253KB.
Tokens: post-login.
Cache hits: 13: 0.00173s for 26,726 bytes (show)
Cache misses: 2: (show)
Queries used: 15.

[Show Queries]