Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VBA Code to position text on connector line

330 views
Skip to first unread message

jimthompson5802

unread,
Dec 8, 2009, 9:59:01 AM12/8/09
to
I'm using VBA to draw a diagram that connects shapes with the dynamic
connector. The default behaviour is to display the connector's TEXT
attribute at the halfway point of the connector.

For different connectors I'd like to position their respective TEXT field at
different points along the connector. Ideal for me is to specify some
percentage (0 to 100) of the length of the connector to position the text.
For example, if I specify 25, I'd like the text to be at the 25% distance
from the start of the line, 85 specifies the text is at 85% distance from the
start of the line.

Another ideal capaibility is that the positioning will work regardless of
the shape of the connector (straight, curved, righ-angle).

Is there an existing Visio object method that performs the above or
something similar? Or is there VBA code fragement that I can adapt for me
needs?

Any pointers will be appreciated.

Jim T

Chris Roth [Visio MVP]

unread,
Dec 8, 2009, 10:49:59 AM12/8/09
to
Hi Jim,

If you make a connector straight, then position the text, Visio will try to
maintain that original percentage-along-the-line. So you can pre-create
connectors with pre-set text positions.

If you are working with existing connectors, it gets a bit trickier. You can
analyze the geometry of a shape using the Curves and Points properties, or
you can use the trick mentioned above in code. Here is a simple bit of VBA
that does this.

Note, this will break glued ends and not repair them, but perhaps there is
something to be learned from the code:

Sub PositionTextOnConnector()

'// Assumes a valid connector is selected:
Dim shp As Visio.Shape
Set shp = Visio.ActiveWindow.Selection(1)

'// Get the current end points:
Dim x0 As Double, y0 As Double, x1 As Double, y1 As Double

x0 = shp.Cells("BeginX").ResultIU
y0 = shp.Cells("BeginY").ResultIU
x1 = shp.Cells("EndX").ResultIU
y1 = shp.Cells("EndY").ResultIU

'// Make it a straight connector, way off the page,
'// 1-inch long:
shp.Cells("BeginX").ResultIU = -10
shp.Cells("BeginY").ResultIU = 0
shp.Cells("EndX").ResultIU = -9
shp.Cells("EndY").ResultIU = 0

DoEvents '// Allow Visio time to catch-up


'// Set the text as a fraction of the way from begin
'// to end:

'Call m_setTextPositionAsFraction(shp, 0.75)
'Call m_setTextPositionAsFraction(shp, 0.25)
Call m_setTextPositionAsFraction(shp, 0.9)


DoEvents '// Allow Visio time to catch-up

'// Restore the original end points
shp.Cells("BeginX").ResultIU = x0
shp.Cells("BeginY").ResultIU = y0
shp.Cells("EndX").ResultIU = x1
shp.Cells("EndY").ResultIU = y1

End Sub

Private Sub m_setTextPositionAsFraction(ByRef shp As Visio.Shape, _
ByVal f As Double)

shp.Cells("Controls.TextPosition").ResultIU = f
shp.Cells("Controls.TextPosition.Y").ResultIU =
shp.Cells("Height").ResultIU / 2

End Sub


I think that Visio 2010 has some new ShapeSheet functions that will make
what you want to do easier/possible from within the shape itself!

--

Hope this helps,

Chris Roth
Visio MVP

For in-depth Visio articles, Visio tutorials and free Visio shapes, stencils
and templates, check out my web site:

Visio Guy
http://www.visguy.com

For more Visio discussion, visit our forum (complete with pictures and
downloads!):

Visio Guy Forum
http://www.visguy.com/vgforum

"jimthompson5802" <jimthom...@discussions.microsoft.com> wrote in
message news:7A87D1BE-94C3-4065...@microsoft.com...

Andy

unread,
Dec 8, 2009, 10:56:25 AM12/8/09
to
If your using your own dynamic connector shapes, you could position
the corresponding text in your shape, Visio then attempts to maintain
this whatever the type of connector, e.g. Dynamic Connector 25,
Dynamic Connector 50 etc with the text position set appropriately. I
couldn't find a sure way of calculating the position of the text
control point for curved lines etc. Visio does a much better job at
maintaining this.

On 8 Dec, 14:59, jimthompson5802

Message has been deleted
Message has been deleted
Message has been deleted
0 new messages