Visio Guy

Visio Discussions => Programming & Code => Topic started by: smelvadj on February 13, 2019, 07:20:49 AM

Title: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
Post by: smelvadj on February 13, 2019, 07:20:49 AM
Hello!

I have read many topic with a similar question. Yet, this have not solved my problem.
I have some code which interchanges data between connected shapes. When the connection of shapes changes, i therefore need to run the VBA Macro again.

Do you have a method to run the macro in "ThisDocument" every time a connection is changed, every time a shapes i moved, or similar ?

Thank you very much!
Title: Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
Post by: wapperdude on February 13, 2019, 05:58:06 PM
I've not done this, but would recommend against.  Such a macro will fire on every shape that is moved or changed.  This will cause Visio to run very slowly.  You will not be happy with the degraded performance.

Anyway, that's my 2 cents worth. 

Wapperdude
Title: Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
Post by: smelvadj on February 18, 2019, 05:25:55 PM
Thank you for the reply.

What would you then recommend. I want the shape information to update automatically, and not by clicking a button.
Title: Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
Post by: wapperdude on February 18, 2019, 07:37:28 PM
I wouldn't do it automatically.  By its nature, it's intrusive and makes the drawing process very slow.  Perhaps the changes were a bad idea, you might not want to save/update those.  Sometimes a move is temporary, and shape gets put back to where it was.  Don't need to save/update that.

The options would be:
1) have the macro fire every 10 minutes...still not my preference...captures things that may not be worth/need capturing.
2) have ability to run macro at users choice...a good feature.  But, probably needs additional capability
3) watch to see if document changed, if so, then fire macro before document closes.  A full update at end of session.

I'm sure there are other scenarios...these come to mind.



Title: Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
Post by: smelvadj on February 18, 2019, 08:58:45 PM
Hi wapperdude,

Thank you for the proposals.

Is it possible to run the macro if a shape connection is changed ?

I connect my shapes with connection points. When i connect one shape to another, my macro copy data from the new shape to the other shape. If i disconnect the shapes again, the data for the shapes are reset by the macro.
Title: Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
Post by: wapperdude on February 19, 2019, 01:54:56 AM
Here's link to various events available:  https://docs.microsoft.com/en-us/office/vba/visio/concepts/event-codesvisio

Here's link to forum discussion on creating shape events code:  http://visguy.com/vgforum/index.php?topic=3392.0

HTH
Title: Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
Post by: smelvadj on February 19, 2019, 01:18:05 PM
Hi,

I will now explain what i need to do. The previous solutions does not fit my problem.

By now, i have two connection points on each shape, one in-going connection point and one outgoing connection point.
When i connect one shapes out-going connection point with one shapes in-going connection point a macro should be fired to copy data from the out-going to the in-going.
If the shape connection is then broken, the macro should be fired again.
In some cases i need to connect eg. 5 shapes. In this case the 4 shapes should "copy" some shape data from last shape which only have a shape connected to its in-going connection point.

I hope you have some solutions for this problem. I look forward to hear from you!
Title: Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
Post by: Croc on February 19, 2019, 02:37:59 PM
A few years ago, I did something like this.
I used page_ConnectionsAdded, page_ConnectionsDeleted events
Title: Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
Post by: smelvadj on February 19, 2019, 04:24:34 PM
Thanks! I have figured it out by using ConnectionsDeleted / ConnectionsAdded!

I have a new problem.

Is it possible to write new data to a shape if i know it's shape ID ?

Eg. i know that i need to write new data to the shape with ID 78.

Thank you for the help!
Title: Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
Post by: wapperdude on February 19, 2019, 04:58:24 PM
Yes.

In general, here are the 3 methods typically used.  The middle one is what you want.


    Dim vsoShp as Visio.Shape

    Set vsoShp = ActiveWindow.Selection(1)    'Assumes only desired shape is selected
    Set vsoShp = ActivePage.Shapes.ItemFromID(1)   'Assumes, in this example, the shape is sheet.1
    Set vsoShp = ActivePage.Shapes.Item("Circuit and Title Block")   'Least preferred, shape naming has issues