Adding Connectors to AutoShapes

N

Nick Hebb

I'm trying to add some shapes to a canvas, but I'm having trouble
getting the connectors to be positioned properly. The code needs to run
under Word 2000 and 2002, and the object models don't support some of
the newer properties like ConnectorFormat that would make this easier.

Here is some demo code of what I'm trying to do:

Sub AddShapesDemo()

Dim doc As Document
Dim cnvs As Shape
Dim shp1 As Shape
Dim shp2 As Shape
Dim con As Shape

Dim l As Single
Dim t As Single
Dim w As Single
Dim h As Single

Dim x1 As Single
Dim y1 As Single
Dim x2 As Single
Dim y2 As Single

Set doc = ActiveDocument

l = doc.PageSetup.LeftMargin
t = doc.PageSetup.TopMargin
w = Application.InchesToPoints(6)
h = Application.InchesToPoints(9)

Set cnvs = doc.Shapes.AddCanvas(l, t, w, h)

' const 61 = msoShapeFlowchartProcess
Set shp1 = cnvs.CanvasItems.AddShape(61, 20, 20, 96, 25.5)
shp1.TextFrame.TextRange.Text = "Some text"

Set shp2 = cnvs.CanvasItems.AddShape(61, 20, 71.5, 96, 25.5)
shp2.TextFrame.TextRange.Text = "More text"

x1 = shp1.Left + shp1.Width / 2
y1 = shp1.Top + shp1.Height
x2 = shp2.Left + shp2.Width / 2
y2 = shp2.Top

' const 2 = msoConnectorElbow
Set con = cnvs.CanvasItems.AddConnector(2, x1, y1, x2, y2)

Set shp1 = Nothing
Set shp2 = Nothing
Set con = Nothing
Set cnvs = Nothing
Set doc = Nothing

End Sub


The problem is, after the code runs, the connector is positioned
horizontally in the middle of the first shape. Anyone know what the
problem with the code is and how I can correct it?


Thanks,

Nick Hebb
 
Z

zkid

Not sure if this will help at all, but have you tried turning off the canvas
feature (i.e, through tools, options)? Maybe I'm just unwilling to change,
but the canvas frustrates the heck out of me when I work with graphics
manually.

Just a thought....
 
N

Nick Hebb

zkid, I tried your suggestion, but unfortunately it didn't work.

What did work a little better was to set the x2 dimension to 0.
Apparently the coordinates are relative. Also I think the problem is
the anchor point. I don't think the anchor point for the line is the
same as the shapes.

I'm only guessing this based on some one other thing that is happening.
When I run the code the first time after opening the file, it places
the canvas right at the left and top margins. If I delete the canvas
and run it again, the left and top positions of the canvas shift to the
right and down, respectively.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top