Вы находитесь на странице: 1из 3

Building Freeform Shapes in Excel (Crazy Stuff!

)
I have been programming in VBA and Excel since the time it first existed (yeah, Im over 50)
and I have never run across such a strange set of commands as the freeform building process. It
defies all logic.
That said, lets figure out the way to use this bad boy so that we can put it to good use. First,
forget about anything logical and let yourself believe what will happen. Second, start with very
simple freeforms, ie. two endpoints, or you will not be able to understand what happens.
Background: A freeform is the drawing shape that consists of points (nodes) connected by lines
(which can be curves or straight lines). The curvature of the curvy lines is described by slope
points. This is like using splines where the location of the point is defined (x,y) and the slope of
the line before (dx/dy before) and after (dx/dy after) are defined. Most spline routines assume a
smooth transition across the point, however this is not necessarily the case with freeforms since
you can define a corner with different slopes coming in and going out (thats what makes the
corner). Freeforms are easy to draw as shapes in Excel or Word or Powerpoint. They can be
further manipulated by editing single nodes (points) and manipulating the location of the point
(x,y) and the slopes (dx/dy) coming in and going out of the point.
Using the freeform shape in VBA.
The freeform shape is created by a method called buildfreefrom as shown in the snippet below
Set wksData = Worksheets("Sheet1")
With wksData.Shapes.BuildFreeform(msoEditingCorner, x1, y1)
.AddNodes msoSegmentCurve, msoEditingCorner, x2, y2, x3, y3,
x4, y4
.ConvertToShape
End With
Note that the AddNodes method is one line (wrapped here to fit the margins). What I did before
this is read from the worksheet values for x1, y1, x2, y2 etc..and then invoke this command. It
turns out that there are very different behaviors depending on how much information you send to
the addnodes method. This is not explained in the Microsoft Help anywhere.
What I will demonstrate is a progressively more complicate line (just two nodes, but with more
defined curvature). What I did on the spreadsheet was something like this:
x1
100

y1
100

x2
200

y2
100

x3
0

y3
0

x4
0

y4
0

So the VBA routine just read the data from left to right. For this example the line would look
something like this:

Just a horizontal line starting at 100 x, 100 y units from the upper left corner of the spreadsheet
and ending at location (200,100). Remember that this is the windows convention of an origin in
the upper left corner of the window. There is obviously no curvature or other magic here.
The second line is generated with a little more information
x1
100

y1
100

x2
100

y2
50

x3
200

y3
100

x4
0

y4
0

Notice that the (x,y) location of the second node has moved down to x3, y3 position. The x2, y2
position now has the information for slope of the first point. Here is the new line:

Note that the slope at the first node is vertical because x2, y2 is vertcal compared to x1,y1. The
slope at x2,y2 is dictated by the mathematics of the curve, probably parabolic or cubic, I dont
know.
The third line contains all the location and slope information possible. Once again the x2, y2
location data is bumped down the line and slope data is inserted just before.
x1
100

y1
100

x2
100

y2
50

x3
150

y3
150

x4
200

y4
100

Once again x1,y1 is the location of the first node, x2,y2 is the control point for the slope coming
off the first node, x3,y3 is the control point for the slope coming off the second node, finally
x4,y4 is the location of the second node. I included a sketch below to illustrate all the coordinates
that are mentioned in the the discussion.
X
100,50
Y

200,100
100,100

150,150

So, the coordinates of the control lines (dx/dy) are absolute with respect to the origin, not relative
to the point they define. To manipulate the relative values, one must add in the location of the
point of interest (x,y) to the relative slope number (dx/dy). Note that the slope control lines do
not have to be the values I have shown, however, the longer the slope control line, the greater
power it has on the overall behavior of the freeform segment. Note the difference here when I
make the slope control for node 1 slightly larger
x1
100

y1
100

x2
100

y2
25

x3
150

y3
150

x4
200

y4
100

Note that y2 is now 25, meaning that the vertical slope control line is 25 units longer than
previous shown by the dashed line.

Вам также может понравиться