Visio Guy

Visio Discussions => Programming & Code => Topic started by: Visisthebest on July 30, 2018, 04:45:51 PM

Title: VBA for swapping two connected shapes
Post by: Visisthebest on July 30, 2018, 04:45:51 PM
Hi All,

Just found this forum, how great this resource exists!

I am new to VBA and Visio, have been exposed much more to Javascript (all the good and the bad of JS).

We have a huge flowchart diagram where a lot of shapes are connected from left to right, each one with an incoming connection and an outgoing connection.

These shapes have a time in seconds or even milliseconds associated with them (they are events). They should be chronological but people people can make the mistake of sometimes putting an event out of order, most often meaning that one event connected to the other (directly) needs to be swapped to repair the chronological order.

What is important to know and to consider before starting out to write some VBA code to swap two events connected next to each other? The other connections these events have with other shapes should also be connected to the swapped shape. The shapes have identical sizes (rectangles). All the data on these shapes needs to move along with the swap.

Your ideas for how to start out making some VBA for this are greatly appreciated.

Warm regards,

Max Larsen
Title: Re: VBA for swapping two connected shapes
Post by: wapperdude on July 31, 2018, 01:00:43 AM
Take a look at Paul Herber's Super Utilities.   

Wapperdude
Title: Re: VBA for swapping two connected shapes
Post by: Visisthebest on July 31, 2018, 11:48:22 AM
Wapperdude thank you great functionalities in that tool kit, but it doesn't do shape swapping and reconnecting unfortunately.
Title: Re: VBA for swapping two connected shapes
Post by: metuemre on July 31, 2018, 01:08:54 PM
It will be helpful if you can provide a sample visio file. What you want can be done with VBA
Title: Re: VBA for swapping two connected shapes
Post by: Paul Herber on July 31, 2018, 01:34:46 PM
Quote from: wapperdude on July 31, 2018, 01:00:43 AM
Take a look at Paul Herber's Super Utilities.   

Wapperdude
thanks dude,. I'm sorry but my utilities can't do that.
Title: Re: VBA for swapping two connected shapes
Post by: Visisthebest on July 31, 2018, 02:02:01 PM
Great tools Paul will probably buy them!

I will make a sample file, but it is virtually identical to a cross-functional flowchart but with fine-grained times on the columns. (for instance, from electronic aviation system logs and other data sources for doing accident investigation, challenge is that the examples I have are confidential need to make an example without real data)

The swap is for horizontally-connected shapes that need to be swapped.

Thanks!

Max
Title: Re: VBA for swapping two connected shapes
Post by: wapperdude on July 31, 2018, 02:21:14 PM
As I have V2007, haven't explored this myself.  But, with V2013, there is a change shape feature that might meet your needs.  https://blogs.msdn.microsoft.com/officedevdocs/2012/08/23/change-shapes-in-visio-2013/ (https://blogs.msdn.microsoft.com/officedevdocs/2012/08/23/change-shapes-in-visio-2013/)

HTH
Wapperdude
Title: Re: VBA for swapping two connected shapes
Post by: Visisthebest on August 01, 2018, 12:34:29 AM
Yes Wapperdude thank you but I am looking for almost the inverse of that functionality, all the shape data and properties should move with the swap rather than stay put. I'll experiment with this in the coming weeks and share my code when it works, I'll write it generically so it works on cross-functional flowchart type diagrams and similar.
Title: Re: VBA for swapping two connected shapes
Post by: AJD on August 05, 2018, 08:24:22 PM
Spit-balling here, don't have the opportunity to test this out any time soon.

You imply that the connectors are directional, so you could perform a check to see if the <timevalue> of the object on the left (the incoming connector) is less than the <timevalue> of the current object. Similar checks for the object on the right. This is a simple pair of YES/NO that could be displayed graphically (e.g. change the colour of the shapes etc).

To make life easier, a custom shape (I assume you are using those anyway) that constrains the types and directions of connectors (perhaps even custom connectors). If using standard shapes and connectors, you can determine visual direction by identifying which end of the connector is connected to which shape and then identifying which end has the arrow head. But, constraining the shapes makes these checks easier.

This is based on some work I did a few years ago (hence lack of fine detail here) to traverse a connected graph which used semi-standard connectors that the users could swap and modify in the UI.

Title: Re: VBA for swapping two connected shapes
Post by: Visisthebest on August 07, 2018, 12:06:54 PM
Thank you for this very useful to think about these options!
Title: Re: VBA for swapping two connected shapes
Post by: pavane on September 09, 2018, 07:10:29 AM
Can you not just swap the contents?
Title: Re: VBA for swapping two connected shapes
Post by: Visisthebest on September 16, 2018, 12:12:27 PM
Quote from: pavane on September 09, 2018, 07:10:29 AM
Can you not just swap the contents?

Panave yes that is a possibility too, do you expect that to be faster as well? What is a good way to do this in VBA so it is truly one transaction, if any part of it doesn't execute properly (which would corrupt the data) the entire transaction is (automatically for the user) reversed and the user gets a prompt informing him/her the swap hasn't gone through?
Title: Re: VBA for swapping two connected shapes
Post by: Paul Herber on September 16, 2018, 02:33:41 PM
First, set the Undo scope, so that if it goes wrong then all you have to do apply the undo and it will happen.
Next, it will depend on what shape properties you need to swap. Are the shapes of the same type? Is it just text to be swapped?
Title: Re: VBA for swapping two connected shapes
Post by: Visisthebest on September 16, 2018, 11:22:22 PM
Thank you Paul, same shapes yes, both text and shape data need to be swapped.
Title: Re: VBA for swapping two connected shapes
Post by: Visisthebest on September 16, 2018, 11:25:29 PM
Oh yes I forget there may be more causal links than just on the horizontal line, so some connections may have to be reconnected from the one shape to the other and vice versa if just the properties of the shape are moved over.