2

I'm writing a code generation tool using VBA in Excel (don't ask why—long story). I need to be able to "parse" a flowchart.

The problem is that Excel allows shapes to contain text, with the exception of connectors: lines and arrows can't contain text. To label an arrow, you just put a text box on top of it—but the box isn't "attached" to the arrow in a way that VBA can easily capture.

For example, a user might draw something like this:

example flowchart: three boxes, two arrows, two more boxes as arrow labels

Within my VBA code, I can use ActiveSheet.Shapes to find that the flowchart contains seven shapes: there are five boxes (the two labels are just boxes with no border) and two arrows. Then Shape.TextFrame2 will tell me what's written inside each box, and Shape.ConnectorFormat will tell me which box goes at the start and end of each arrow.

What I need is code that can deduce:

  • Label A belongs to the arrow from Box 1 to Box 2
  • Label B belongs to the arrow from Box 1 to Box 3

I can think of three ways of doing this, none of them satisfactory.

  1. Ask the user to group each label with its corresponding arrow.

  2. Find out the coordinates of the endpoints of each arrow, then calculate which arrows pass through which labels.

  3. Find out the coordinates of the corners of each box, then calculate which labels lie between which pairs of boxes.

Method 1 makes things easier for the programmer but harder for the user. It opens up a lot of potential for user error. I don't see this as an acceptable solution.

Method 2 would be reasonably easy to implement, except that I don't know how to find out the coordinates!

Method 3 is doable (Shape.Left etc will give the coordinates) but computationally quite messy. It also has potential for ambiguity (depending on placement, the same label may be associated with more than one arrow).

Note that methods 2 and 3 both involve trying to match every label with every arrow: the complexity is quadratic. Typical applications will have 10–50 arrows, so this approach is feasible, if somewhat inelegant.

Does anyone have a better idea? Ideally it would be something that doesn't involve coordinate geometry and complicated logic, and doesn't involve asking users to change the way they draw flowcharts.


Edited to add: example 2 in response to Tim Williams

flowchart with one box, two arrows, and a label whose bounding box intersects the bounding boxes of both arrows

Here's a label whose bounding box intersects the bounding box of both arrows, and whose midpoint isn't inside the bounding box of either arrow. Visually it's easy for a human to see that it belongs with the left arrow, but programmatically it's hard to deal with. If I can find out the coordinates of the arrows' endpoints, then I can calculate that one arrow passes through the label's box but the other doesn't. But if all I have is the bounding rectangles of the arrows, then it doesn't work.

5
  • 1
    Option #2 seems like the way to go - the line has Top, Left, Width and Height properties, as does the Textbox shape, so you should be able to use these to figure out if the textbox is contained in the rectangle bounding the line. You could allow some wiggle room for labels which extend beyond the line's bounds. Maybe check to see if the mid-point of the label is within the line's bounds. Dec 4, 2013 at 0:26
  • Thanks for the suggestion. It's not perfect—see the diagram I've added to the end of the post—but it might be the best practical solution. Dec 4, 2013 at 1:34
  • Well, now it looks difficult ;-) Tricky bit also is figuring out which direction the connector runs within the bounding box: if they are actual connectors (anchored to the shapes) then you might be able to figure it out via the line's ConnectorFormat (but a quick look shows that might be not straightforward if there are multiple types of shapes and not just rectangles) Dec 4, 2013 at 1:58
  • Something that works just for rectangles would be good enough, so I think the direction issue is OK. Dec 4, 2013 at 2:43
  • 1
    ask users to group labels with arrows and add some code positioning the center of each label to the middle of the arrow ... for both it would be .Top + 0.5 * Height, Left + 0.5 * Width. This way the user also sees which labels are not grouped, if you position ungrouped labels to e.g. 0, 0 ...
    – MikeD
    Dec 4, 2013 at 12:54

2 Answers 2

1

Interesting problem. What if you considered the range covered by the arrow and the range covered by the textbox and matched them up based on the most overlap.

Sub ListShapes()

    Dim shp As Shape
    Dim shpArrow As Shape
    Dim vaArrows As Variant
    Dim i As Long
    Dim rIntersect As Range
    Dim aBestFit() As String
    Dim lMax As Long

    vaArrows = Split("Straight Arrow Connector 7,Straight Arrow Connector 9", ",")
    ReDim aBestFit(LBound(vaArrows) To UBound(vaArrows))

    For i = LBound(vaArrows) To UBound(vaArrows)
        Set shpArrow = Sheet1.Shapes(vaArrows(i))
        lMax = 0

        For Each shp In Sheet1.Shapes
            If shp.Name Like "Label*" Then
                Set rIntersect = Intersect(Sheet1.Range(shp.TopLeftCell, shp.BottomRightCell), _
                    Sheet1.Range(shpArrow.TopLeftCell, shpArrow.BottomRightCell))

                If Not rIntersect Is Nothing Then
                    If rIntersect.Count > lMax Then
                        lMax = rIntersect.Count
                        aBestFit(i) = shp.Name
                    End If
                End If
            End If
        Next shp
    Next i

    For i = LBound(vaArrows) To UBound(vaArrows)
        Debug.Print vaArrows(i), aBestFit(i)
    Next i

End Sub

I tested this with the five box-two arrow setup and nothing more complicated. I put my two arrows in an array, but I assume you have ways to identify the arrows. I also named my untethered boxes "Label x" so I could identify them, but again I assume you have something more sophisticated.

The code loops through every arrow. Inside that loop, it loops through every shape. If it's a label, then it counts the cells in the intersection of the two ranges. Whichever has the most is stored in the best fit array.

It would be nice if you had a reasonable corpus of flow charts to test this to see where the pitfalls are. I don't think this is necessarily better than use the coordinates, just a different approach.

1
  • It's a neat idea, but not very robust. There are problems with the fact that vertical arrows cover a narrower range of cells than diagonal arrows, and also with ties being broken according to which arrow is first in the list. Try the second example in my post: you'll get different results according to which arrows comes first in your array, and according to how the arrows line up with the cells underneath them in the spreadsheet. Thanks for the suggestion though—it's possible that some variation on this theme might be useful. Dec 6, 2013 at 6:28
1

You can find the coordinates of the arrow's endpoints as follows.

First of all, the .Left, .Top, .Width and .Height properties describe the bounding rectangle of the arrow, as Tim Williams points out.

Next, check the .HorizontalFlip and .VerticalFlip properties. If both are false, then the arrow runs from top left to bottom right in its bounding rectangle. That is, the beginning of the arrow has coordinates (.Left,.Top) and the end has coordinates (.Left+.Width,.Top+.Height).

If either *.Flip is true, then the coordinates need to be swapped around as appropriate. E.g., if .HorizontalFlip is true but .VerticalFlip false, then the arrow runs from (.Left+.Width,.Top) to (.Left,.Top+.Height).

As far as I can tell, this is not documented anywhere on MSDN. Thanks to Andy Pope for mentioning it at excelforums.com.

Given this, method 2 seems like the best approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.