How to connect an Autoshape Connector to a Shape in VBA?

V

Vincent Verheul

Hello Group,

I have create VBA code to generate Rectangle Shapes and Autoshape Connectors
on a Canvas. This works fine, but the Autoshape Connectors are not
*connected* to the Rectangle Shapes.

I am using the <canvasobject>.CanvasItems.AddShape () and
<canvasobject>.CanvasItems.AddConnector() methods. I am looking for a
property or method of the connector object that allows me to specify another
object (i.e. my Rectangle Shape): one connection for the head and one
connection for the tail.

TIA
Vincent Verheul
 
J

Jean-Guy Marcil

Vincent Verheul was telling us:
Vincent Verheul nous racontait que :
Hello Group,

I have create VBA code to generate Rectangle Shapes and Autoshape
Connectors on a Canvas. This works fine, but the Autoshape Connectors
are not *connected* to the Rectangle Shapes.

I am using the <canvasobject>.CanvasItems.AddShape () and
<canvasobject>.CanvasItems.AddConnector() methods. I am looking for a
property or method of the connector object that allows me to specify
another object (i.e. my Rectangle Shape): one connection for the head
and one connection for the tail.

I do not think it works like that. When you create a connector, you have to
specify a start point and an end point. So, if you create all the shapes
with you code it is just a matter of specifying exact position. Try with
this code:

'_______________________________________
Dim shpCanvas As Shape

Set shpCanvas = ActiveDocument.Shapes.AddCanvas( _
Top:=75, Left:=100, Width:=200, Height:=200)
With shpCanvas.CanvasItems
.AddShape Type:=msoShapeRectangle, Top:=25, _
Left:=25, Width:=50, Height:=30
.AddConnector msoConnectorStraight, 75, 40, 25, 0
.AddShape Type:=msoShapeRectangle, Top:=25, _
Left:=100, Width:=50, Height:=30
.AddConnector msoConnectorStraight, 125, 55, 0, 25
.AddShape Type:=msoShapeRectangle, Top:=80, _
Left:=100, Width:=50, Height:=30
.AddConnector msoConnectorStraight, 100, 95, -25, 0
.AddShape Type:=msoShapeRectangle, Top:=80, _
Left:=25, Width:=50, Height:=30
.AddConnector msoConnectorStraight, 50, 80, 0, -25
'Group and colour the shapes
With .Range(Array(1, 2, 3, 4, 5, 6, 7, 8)).Group
.Fill.PresetTextured msoTexturePapyrus
.GroupItems(7).Fill.PresetTextured msoTextureWovenMat
End With
End With
'_______________________________________

One point is that in the VBA help they state that the AddConnector method
requires the following parameters:
AddConnector(Type, BeginX, BeginY, EndX, EndY)
where BeginX and EndX would seem to indicate absolute start and end point
for the connector.

In fact, the start point is an absolute position, but the end point is in
fact an offset position in relation to the start point.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
V

Vincent Verheul

Hello Jean-Guy,

Thanks for your response. I already use code that you illustrated (except
for the grouping) and I am aware of the fact the connector uses an absolute
beginning and a relative (offset) ending in its specification.

The resulting image looks neat, but there is still a problem: you can not
drag a rectangle to another position without losing the connectors. You can
see that the connectors are not *connected* to the rectangles in Word when
you select a connector: the endings are *Green* where I want them to be
*Red*. A Red connector junction indicates that it is connected to the shape
(an not just positioned next to it).

An important effect of a connector being *connected* (I use an Elbow
connector) is that it is shaped neatly between the Rectangles without having
a portion of it running in parallel to a side of the rectangle for instance.

I think there should somehow be a link between the connector and the
Rectangle shapes that it is to connect to.

Any ideas?

TIA
Vincent
 
J

Jean-Guy Marcil

Vincent Verheul was telling us:
Vincent Verheul nous racontait que :
Hello Jean-Guy,

Thanks for your response. I already use code that you illustrated
(except for the grouping) and I am aware of the fact the connector
uses an absolute beginning and a relative (offset) ending in its
specification.
The resulting image looks neat, but there is still a problem: you can
not drag a rectangle to another position without losing the
connectors. You can see that the connectors are not *connected* to
the rectangles in Word when you select a connector: the endings are
*Green* where I want them to be *Red*. A Red connector junction
indicates that it is connected to the shape (an not just positioned
next to it).
An important effect of a connector being *connected* (I use an Elbow
connector) is that it is shaped neatly between the Rectangles without
having a portion of it running in parallel to a side of the rectangle
for instance.
I think there should somehow be a link between the connector and the
Rectangle shapes that it is to connect to.

Any ideas?

Sorry, no.

If you add a connector and record it you get this:
'_______________________________________
Sub Macro3()
'
' Macro3 Macro
' Macro recorded 6/16/2006 by Jean-Guy Marcil
'
ActiveDocument.Shapes.AddConnector(msoConnectorStraight, 270#, 189.05, _
81#, 8.95).Select
Selection.ShapeRange.Flip msoFlipHorizontal
Selection.ShapeRange.Flip msoFlipVertical
Selection.ShapeRange.ConnectorFormat.BeginConnect
ActiveDocument.Shapes( _
"Rectangle 126"), 4
Selection.ShapeRange.ConnectorFormat.EndConnect ActiveDocument.Shapes( _
"Rectangle 127"), 2
End Sub
'_______________________________________

Now, delete the connector you just added and run the exact same recorded
code again, you get an error.

I managed to get:

'_______________________________________
Dim shpCanvas As Shape
Dim shpConnect As Shape
Dim shpBox1 As Shape
Dim shpBox2 As Shape

Set shpCanvas = ActiveDocument.Shapes.AddCanvas( _
Top:=75, Left:=100, Width:=200, Height:=200)
shpCanvas.Name = "Canvas"
With shpCanvas.CanvasItems
Set shpBox1 = .AddShape(Type:=msoShapeRectangle, Top:=25, _
Left:=25, Width:=50, Height:=30)
shpBox1.Name = "Rec1"
Set shpBox2 = .AddShape(Type:=msoShapeRectangle, Top:=25, _
Left:=100, Width:=50, Height:=30)
shpBox1.Name = "Rec2"
Set shpConnect = .AddConnector(msoConnectorStraight, 75, 40, 25, 0)
End With
shpConnect.Select
Selection.ShapeRange.ConnectorFormat.BeginConnect shpBox1, 3
Selection.ShapeRange.ConnectorFormat.EndConnect shpBox2, 1
'_______________________________________

But I get an error on the last two lines because (according to Word and even
if I used the .AddConnector method) the selected object is not a connector.

I think these properties work in Excel or PowerPoint but were poorly
implemented in Word.
Nothing about them in the online help by the way.

For example, this works in Excel:

'_______________________________________
Dim shpConnect As Shape
Dim shpBox1 As Shape
Dim shpBox2 As Shape


With Worksheets(1).Shapes
Set shpBox1 = .AddShape(Type:=msoShapeRectangle, Top:=25, _
Left:=25, Width:=50, Height:=30)
shpBox1.Name = "Rec1"
Set shpBox2 = .AddShape(Type:=msoShapeRectangle, Top:=25, _
Left:=100, Width:=50, Height:=30)
shpBox1.Name = "Rec2"
Set shpConnect = .AddConnector(msoConnectorStraight, 75, 40, 25, 0)
End With
With shpConnect
.ConnectorFormat.BeginConnect shpBox1, 3
.ConnectorFormat.EndConnect shpBox2, 1
End With
'_______________________________________

It is a shame because there are may properties to the ConnectorFormat you
would have been able to use. (Like accessing the parent shapes, the
connected sites, etc.)

May be you could create the whole drawing in Excel, then import it in a Word
canvas (In Word you need a canvas for the connector stuff to work, not in
Excel)...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
V

Vincent Verheul

Hello Jean-Guy,

Thanks for the research work! Indeed a shame that this piece of
functionality seems to be missing in the Word Object model. I will try this
in PowerPoint. I also want to save the result in a .gif file. This is
possible in Word when saving as HTML page, but in Powerpoint you can
directly save as .gif file I just noticed! I use the .gif file in HTML
output that I generate out of an MsAccess database (the drawing is also
generated from data out of that database). Well there is some work for me to
get it working in PPT now.

Thanks again!
Vincent
 

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