[Tutorial] Auto ungroup shapes on drop

Started by Lars-Erik, April 28, 2008, 06:53:24 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


I wanted to make it possible for users to add a group of shapes with just one drag. This way sub systems can be easily added and they would already have the right components, and the right lay-out.
On default though, adding multiple shapes to a stencil (as 1 shape) will make Visio group them.

On the the Microsoft site i found an article telling me that DOCMD(1052) ungroups a shape. When adding this command to the on drop even in the shapes shapesheet it will automatically be ungrouped on dropping it in a drawing. This has one small drawback though, it asks the user for input because ungrouping can cause data loss (the top group shapesheet is removed).

Using a macro you can automate is so that you don't see anything and still get the shape ungrouped.

1- Write a macro:

Sub UngroupThis(shpObj As Visio.Shape)
On Error GoTo A
'Respond OK to all messages
Application.AlertResponse = 1
'Ungroup the shape
'Stop auto responding to messages
'When macro fails settings will be restored to Visio default
Application.AlertResponse = 0
End Sub

2- Group your "sub system" of shapes that you want to be able to drag onto drawings
3- Add the following code to you new groups on drop event:


All done!

Two small warnings might be in place, this macro doesnt check if the shape is indeed a group (it relies on the user to be smart enough not to add the code to a non group shapesheet. Also ungrouping can not be undone, you WILL lose you data in the top layer shapesheet!

For more information about preventing alert messages in Visio: http://support.microsoft.com/kb/279710

- Lars-Erik

Visio Guy

Nice Lars-Erik!

Another note: when you mess with AlertResponse, be sure to put in some error checking in case something goes wrong. You don't want the AlertResponse to stay at 1 forever - Visio will keep "hitting OK" and never showing you a dialog.

When this happens, it can be VERY confusing, so trap those errors!

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


Added an "On Error" in the original post.
Your right, when for some reason the shape could not be ungrouped (not a group, group is protected) it would give an error and not return the AlertResponse back to default. Should be fixed now. Thanks for pointing this out.

- Lars-Erik


Seems like you could make a "script shape" that defines sizes, positions, etc. of various master shapes. When the shape is dropped, it would call "CallThis" VBA code or some other add-in.

Think of a flowchart-creator example. It could have text like this:

Text="Analyze This!"
Text="You make the call"
Text="Print 'Paperless Office' White-paper"

The code would parse the text and delete the shape. Then it would drop the Process, Decision and Document shapes, and set the cell values for each one.
6'4" and full of muscle


Hopefully this post is still being monitored...

I cant seem to get things to work by using the macro and formula in the drop event.  I'm new to working with custom shapes, the ShapeSheet, and the VBA editor for Visio, so I'm hoping someone can post back with a more step-by-step walkthrough of just how to get this to work.

What I'm trying to accomplish is to create a new custom shape that consists of three boxes (think of it like a 3-box hierarchical diagram with a parent box and two child boxes directly below it).  When creating these as a new master, they get grouped together (this part I understand).  What I'm trying to accomplish is exactly what this thread is about (I want to have the three shapes ungrouped, without any prompt, when dragged onto the page), but I'm not sure just how to do it based on the instructions given by the OP.

Here's what I've tried, but I'm not sure I'm doing it correctly (I'm using Visio 2007 btw):

Create new master and edit shape to add in 3 boxes then group them.
While in the master shape editor page, open VB Editor (Tools > Macro > Visual Basic Editor).
In the VB Editor (not sure if this is correct), right-click "ThisDocument" and choose "View Code".
Paste in the "Sub" you posted above then close the VB editor.
Back in the master shape editor, open the ShapeSheet for the grouped shape (3 boxes).
Add in the "CALLTHIS..." formula to the "EventDrop" then close the ShapeSheet.
Close the master shape editor (clicking "Yes" on the dialog asking to update the shape).

After doing all this, I then try to drag the shape onto a document, but when I do it remains grouped.

So, what am I doing wrong?  Am I not creating the code right?

Any help on this would really be appreciated because I'm so new at this kind of stuff and I have no clue.

Thanks in advance,

- Dink


First off, try this:


If this doesn't help get back to us, I'll explain in detail how to do this. (Maybe a video or something might be easiest)

- Lars


That did the trick  :)

Thanks for the quick response, I really appreciate it.

- Dink


CALLTHIS function is for visio page or grouped shape


how to use CALLTHIS Function in COM INTEROP services for visio 2007 using in C# throws The name CALLTHIS does not exist in the context.

Any information will help me.



I started a new thread on this topic as nothing had been posted for at least 120 days.


Please take a look as I'm having issues with the macro. Note: I am using "CALLTHIS("ThisDocument.UngroupThis")" on the ShapeSheet for the Shape, per the thread.

Essentially, when I drag the shape onto the document the shape remains grouped.

I'm not sure what I'm doing wrong and I need more assistance!