Author Topic: Delete a shapee(s) in VBA macro. Deleting all Cats is VBA  (Read 12895 times)

0 Members and 1 Guest are viewing this topic.

DinoCobolMan

  • Jr. Member
  • **
  • Posts: 19
Delete a shapee(s) in VBA macro. Deleting all Cats is VBA
« on: August 03, 2014, 01:18:05 PM »
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

  • Hero Member
  • *****
  • Posts: 2747
Re: Delete a shapee(s) in VBA macro. Deleting all Cats is VBA
« Reply #1 on: August 03, 2014, 01:53:42 PM »
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:
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

  • Jr. Member
  • **
  • Posts: 19
Re: Delete a shapee(s) in VBA macro. Deleting all Cats is VBA
« Reply #2 on: August 03, 2014, 03:44:19 PM »
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

  • Hero Member
  • *****
  • Posts: 2747
Re: Delete a shapee(s) in VBA macro. Deleting all Cats is VBA
« Reply #3 on: August 03, 2014, 03:55:38 PM »
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

  • Jr. Member
  • **
  • Posts: 19
Re: Delete a shapee(s) in VBA macro. Deleting all Cats is VBA
« Reply #4 on: August 03, 2014, 05:37:55 PM »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4090
  • Ideas Visio-lized into solutions
Re: Delete a shapee(s) in VBA macro. Deleting all Cats is VBA
« Reply #5 on: August 03, 2014, 06:19:40 PM »
The code blows up because your drawing probably has shapes with no masters.  Modify as follows:

Code
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

  • Jr. Member
  • **
  • Posts: 19
Re: Delete a shapee(s) in VBA macro. Deleting all Cats is VBA
« Reply #6 on: August 03, 2014, 08:52:29 PM »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4090
  • Ideas Visio-lized into solutions
Re: Delete a shapee(s) in VBA macro. Deleting all Cats is VBA
« Reply #7 on: August 03, 2014, 09:23:25 PM »
Can you upload a Visio file, doesn't have to have everything.  Something with a cat, and the code.

Wapperdude
Visio 2019 Pro

DinoCobolMan

  • Jr. Member
  • **
  • Posts: 19
Re: Delete a shapee(s) in VBA macro. Deleting all Cats is VBA
« Reply #8 on: August 03, 2014, 09:59:31 PM »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4090
  • Ideas Visio-lized into solutions
Re: Delete a shapee(s) in VBA macro. Deleting all Cats is VBA
« Reply #9 on: August 03, 2014, 11:55:41 PM »
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

  • Jr. Member
  • **
  • Posts: 19
Re: Delete a shapee(s) in VBA macro. Deleting all Cats is VBA
« Reply #10 on: August 04, 2014, 09:08:02 AM »
Wapperdude,
Thanks! I will try what you told me to do when I get home.

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4090
  • Ideas Visio-lized into solutions
Re: Delete a shapee(s) in VBA macro. Deleting all Cats is VBA
« Reply #11 on: August 04, 2014, 09:53:17 PM »
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:
Code
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
« Last Edit: August 05, 2014, 12:19:54 AM by wapperdude »
Visio 2019 Pro

Jumpy

  • Hero Member
  • *****
  • Posts: 1061
Re: Delete a shapee(s) in VBA macro. Deleting all Cats is VBA
« Reply #12 on: August 05, 2014, 03:13:04 AM »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4090
  • Ideas Visio-lized into solutions
Re: Delete a shapee(s) in VBA macro. Deleting all Cats is VBA
« Reply #13 on: August 05, 2014, 09:32:54 AM »
Yeah.  Well it never occurred to me that there would be an index problem at all.   :o
Visio 2019 Pro

DinoCobolMan

  • Jr. Member
  • **
  • Posts: 19
Re: Delete a shapee(s) in VBA macro. Deleting all Cats is VBA
« Reply #14 on: August 05, 2014, 05:23:36 PM »
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