0
votes

I am using Visio as a modelling tool via VBA Macros and UserForms. Shapes of the models can have shapes of the same type as a "child", which then again can have a child, ...

Currently I can only add "top-level" shapes via drag and drop from the stencil. I am using the ShapeAdded event to open an userform and let the user enter the Shape Data.

Shapes which have a parent can only be added via a seperate UserForm which takes the Shape Data and the parent object from an list of the all objects in the model. It then calls a Function that iterates through all shapes on the page until it finds the desired "parent" and then uses the Drop method of the parent object to drop the new shape in the right position. The shape is then also added to the group of the parent shape.

Is there any way I could implement this via Drag and Drop? I.e. can I find out via VBA on which "Parent"-Shape the user placed the new shape so i can group them together programatically? (Also I need the information of the parent-child relations to be synched to a database so I need to get the parent shape object to access its Shape Data).

I want this because it allows the user to directly choose the desired position inside of the parent shape and it is more intuitive to use drag and drop from the stencil then using the userform.

Only option I could think about was to open an Userform after drop of the shape and get the parent information from the user, then find the parent shape and add the new shape to its group --> this relies on the user to chose the same object from the list as he is dropping the shape on, which can lead to problems. I want to automatically detect the shape which is beneath the newly dropped shape and only let the user enter the data of the new shape.

1

1 Answers

2
votes

Normally you use containers for this kind of functionality in Visio, not groups. The containers are much more friendly to the drag-n-drop functions like the one you describe (for example: when a user hovers a shape over some container, it is highlighted, indicating to the user that it's available as a drop target). Also, containers provide specialized API, that allows you to list shapes in the container, add/remove and shapes to/from the container, and resizing features.

Check out this educational video: https://support.office.com/en-us/article/video-organize-shapes-in-containers-75e42442-038a-48f6-9206-b029266cbfc7

Or this Microsoft Visio blog post: https://blogs.msdn.microsoft.com/visio/2009/08/25/organizing-diagrams-with-containers/

Here is an overview of the API: https://developer.microsoft.com/en-us/visio/blogs/container-list-and-callout-api-in-visio-2010/