Author Topic: Run "Private Sub" in "ThisDocument" every time you move a shape or similar  (Read 326 times)

0 Members and 1 Guest are viewing this topic.

smelvadj

  • Newbie
  • *
  • Posts: 8
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!

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3435
  • Ideas Visio-lized into solutions
Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
« Reply #1 on: February 13, 2019, 12: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
Visio 2019 Pro

smelvadj

  • Newbie
  • *
  • Posts: 8
Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
« Reply #2 on: February 18, 2019, 12: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.

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3435
  • Ideas Visio-lized into solutions
Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
« Reply #3 on: February 18, 2019, 02: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.



Visio 2019 Pro

smelvadj

  • Newbie
  • *
  • Posts: 8
Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
« Reply #4 on: February 18, 2019, 03: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.

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3435
  • Ideas Visio-lized into solutions
Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
« Reply #5 on: February 18, 2019, 08:54:56 PM »
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
Visio 2019 Pro

smelvadj

  • Newbie
  • *
  • Posts: 8
Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
« Reply #6 on: February 19, 2019, 08:18:05 AM »
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!

Croc

  • Full Member
  • ***
  • Posts: 159
Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
« Reply #7 on: February 19, 2019, 09:37:59 AM »
A few years ago, I did something like this.
I used page_ConnectionsAdded, page_ConnectionsDeleted events

smelvadj

  • Newbie
  • *
  • Posts: 8
Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
« Reply #8 on: February 19, 2019, 11:24:34 AM »
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!

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3435
  • Ideas Visio-lized into solutions
Re: Run "Private Sub" in "ThisDocument" every time you move a shape or similar
« Reply #9 on: February 19, 2019, 11:58:24 AM »
Yes.

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

Code: [Select]
    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
Visio 2019 Pro