BB code in posts seems to be working again!
I haven't turned on every single tag, so please let me know if there are any that are used/needed but not activated.

Main Menu

Counting all connections leading to a shape

Started by o4sythe, February 19, 2015, 12:35:49 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


Hello all,

New user of visio, I've spent the last hour looking for a solution via Google and this forum and no luck!

I'm looking for a way to count the number of connections that have lead to a particular shape so that I can use this to weight the importance of each process within a flow chart.

For example, if the shape labelled 'Negotiation' has three incoming connections, each of which have one incoming connection of their own, I would want to be told that the 'Negotiation' shape has six connections leading to it - any ideas?

Thanks in advance,


Hi Oli...

It's only necessary to place a post once.  The categories are just vague, collections.  Most everyone just looks at the current posts or uses the search feature as there are so many posts to sort through.

As to your problem, it will take a macro or code.  All connection information is "contained" not in the actual shape but in the connectors attached to the shape.  It could be either a connector or some other 1-D line variant.  In addition, since you really want to explore the number of branches (or roots), connected to a shape, and the number of sub-levels is undefined and can vary, the algorithm needs to be "recursive", I think.

One basic attack would be to select a shape, then loop thru the page connections collection, identify which connectors are attached to that shape, and store both the connector ID, the connector end, and the total number of connections made.  Then, for each attached connector, go thru and find what shapes are attached to the opposite end, store those.  Select each opposite end shape and repeat the process, except now, the total count ought to be reduced by one for each sub-level shape, as it's "lead-in" connection has been previous counted.  The process re-iterates until shapes with only one connection are found, presumably, these are the terminal shapes

Hope this helps.  It will take some programming skills.

Visio 2019 Pro


Thanks for the guidance  wapperdude :-)

That sounds somewhat over my ability at this stage but good to know all the same - Definitely something I can come back to when my programming skills have been improved.

Thomas Winkel


with Visio >= 2010 this is easy, see attached document.



How unfair. Password protect macros? What's it worth? Just show that it can be made?


The code in the .vss contains a lot of garbage. This may prevent an inexperienced user. I just cut it out of the unfinished work and did not clean.
Stencil can be used to counting the connectors, as a "black box".
Password = rbeat
Perhaps is needed to explain, that the second parameter in the line
Set c = ConnServ.stnModule.GetTree(ShpNameID, 2)
specified the direction of connectors.
1 - incoming
2 - outgoing
3 - all


Here's an alternative approach.  It doesn't care about direction of connectors, or even position with in a tree.  It reports all connections in the tree.  It's a bit of a cheat, because, it "tags" each shape and connector with a color, keeps count, ignores redundancies (which the connects object always provides), and then does a "bail out" to exit the macro. 

It does use a redim dynamic array to store shapes.  Could have done same for connectors, but, I thought there might be some merit to the coloring.  Unfortunately, I could not determine a way to dynamically loop thru the analysis, so, punched out of the loop at the end.

There could be some simplification on the redundancy testing, but execution for now is fast.

The non-programmer strikes again!   :o

Visio 2019 Pro


OK.  Fixed the loop issue.  Enhanced the color the 1st level connections are different from the indirect connections.

Visio 2019 Pro


These are really interesting, just spent an hour going through the code inch by inch and Just beginning to get my head around it. I'm super impressed by the extent of the effort you're all putting in to helping me, thank you all very much!

wapperdude, your solution seems closest to what I'm looking for - The two issues I'm still having is that that I need only incoming connections leading to the shape to be counted, also I've so far been unable to replicate the results within my existing Visio 2013 flow chart.

Any ideas?

Thanks again!


I don't have V2013. My understanding is the V2013 has VBA features that are supposed to make incoming / outgoing determination easier.

In your drawing, what measures (if any) do you take to show (establish) if a connection is incoming or outgoing?  If it is merely by the direction of the connector arrowhead, assuming that the arrowhead is always on the "end" and never the "beginning", once you've identified a connector, you can check keep only connectors that end on your shape.  But, that's a little involved unless V2013 really does simplify things.

Not sure why it doesn't work in V2013.  Don't think there's any thing unique to the code.  Did you copy the code to your drawing, or the drawing to my file?  I'd suggest copying the code from my file to your file.  Then, tile your drawing window and the VB window.  You can always step thru the code, (F8), and execute line by line.  You can watch what each step does in the drawing, and also see where the code breaks.

There's probably better debug methods, but this works for me.

Visio 2019 Pro


OK.  That wasn't so bad! 

Here's updated file and code.  There is a variable "m", near the beginning, that is hard coded and sets the incoming / outgoing connector type recognition.  This needs to be set manually.

Visio 2019 Pro


BTW, in V2007, I tried this with basic shapes, flow charts, cross-functional charts, org charts...worked every time.  Tried using connectors or line problem.  Doesn't like have two shapes directly glued together...expected...but, I did not put in a test condition for that case.

So, if your V2013 drawing still fails, either someone else with V2013 will have to come the rescue, or, try saving it as V2007, and I can try and find the problem.

But, the code is very generic, should run fine.

Visio 2019 Pro


wapperdude you are fantastic! This works exactly as I wanted - I had to adjust my diagram slightly as it didn't like counting two connections that originated in the same shape (my start shape goes to two decisions and in a few cases the processes lower down the line connect back to both of these decisions)

That was easily fixed however, I've simply disconnected the start shape from the rest, it was essentially just a title anyway.


It seems I may have spoken a bit too early. You're still pretty fantastic though :-D

I've found there are some unavoidable occasions where one shape has connections to two other shapes when then both link to the same shape on the next tier down. i.e. A links to B and C, B and C both link to D. If I run the macro on D I should get "4" but instead I see:

Run-time error '91':
Object variable or With block variable not set

I'm trying to get my head around the specifics...I think that the macro is attempting to change the colour of 'A' twice, so would the solution be to somehow detect if 'A' is already blue (if blue is the right colour!) and if yes then skip the colour change command.

Very interested to know if I'm on the right lines or not!

Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 402 (show)
Files included: 34 - 1306KB. (show)
Memory used: 1268KB.
Tokens: post-login.
Cache hits: 13: 0.00174s for 26,764 bytes (show)
Cache misses: 2: (show)
Queries used: 15.

[Show Queries]