VBA to get sequential flow of shapes

Started by pe, November 12, 2019, 11:03:39 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

pe

All, first post here, hope someone can help.  Have been googling for answer to no avail.
I'm pretty decent with VBA in Excel and Word but Visio is pretty new to me.
Background:  someone (who's left company) created a very nice Visio process flowchart.  Note:  in our company we're restricted to Visio 2007.
What I need to do is get a simple list of the text in each shape in order the shapes occur in the process flow.  Need this to present as a list in a non-Visio format (eg, slides).
I first tried the code below, thinking I could use the index number for this--but turns out the flowchart author stuck in some shapes (boxes) out of order.  So I thought maybe ordering in terms of X and Y coordinates would help--and it was better, except some boxes are a little higher on the Y axis than their predecessor/parent, so that didn't work.
I'm sure there's a better way for what must be a simple task, but for the life of me can't find it.
Can anyone point me in the right direction? 
Thanks in advance


Sub list_shapes()
Dim sh As Shape, n As Integer, clr, clrfrom
For Each sh In ThisDocument.Pages(2).Shapes
Debug.Print n; "text= "; sh.Text; "shapename= "; sh.Name; "index= "; sh.Index; "shapetype= "; sh.Type; "x-coordinate="; sh.Cells("PinX"); "y-coordinate="; sh.Cells("PinY"); "[shapecolor="; sh.Cells("Fillforegnd")
Next
End Sub

Hey Ken

Pe:

   I had a similar issue, except it was a dataflow, not a process flow; but the solution is the same.

   Define an array where each entry consists of three shape names: the source shape, the line connected to it, and the target shape at the other end of the line.  Step through all the lines on the page, and save the line's shape name  in the array.  For each line you save, extract from its BeginX and EndX shapesheet cells the names of the shapes on either end of the line and save them at the same entry in the array.  When done, you now have a map of the entire flow.  Assuming you know which shape is the start, you can search the array for that name, and you know who it's connected to.  Search the array for that one, and you know who the next one is, etc.  As you step through the flow, you can extract the shape's text as you go along and add it to your list.

   The only issue is that you may not know which end of the line is which, so you can't tell source from target.  But if you add an arrow to each line, you can use that to identify which end is which.  My dataflow already had the arrows; you may have to add them manually.

   This approach is MUCH easier than fiddling around with X/Y coordinates, Visio connections, etc.  Worked for me.  Hope it works for you.

   - Ken
Ken V. Krawchuk
Author
No Dogs on Mars - A Starship Story
http://astarshipstory.com

AJD

For reference: Cross-posted on StackOverflow: https://stackoverflow.com/q/58817608/9101981.

I have created a partial answer: https://stackoverflow.com/a/58844985/9101981, I focussed on Visio 2010 initially but I have now added some older code - "here is one I prepared earlier" - that is probably more complex than needed but don't have the time to rejig just to answer a question.