您似乎没有从您检索的 Connect 对象中获取形状参考。那么这样的事情怎么样:
Set shpConn = shpStart.FromConnects(1).FromSheet
Dim vFromCellStop As Cell
Set vFromCellStop = shpConn.CellsU("EndX")
vFromCellStop.GlueToPos shpStop, 0.5, 0.5
如果您使用的是 Visio 2007,您可能会发现删除原始连接器并在 Shape 对象上使用AutoConnect方法更简单。如果您正在查看以前的版本,或者特别想处理哪些单元格粘合到哪个单元格,那么 Connects 和 FromConnects 会更好。
以下是这两种方法的几个 VBA 示例:
Sub AutoConnectExample()
Dim shpStart As Shape
Dim shpStop As Shape
Dim vPag As Page
Set vPag = ThisDocument.Pages(1)
Set shpStart = vPag.Drop(ThisDocument.Masters("Start"), 1, 3)
Set shpStop = vPag.Drop(ThisDocument.Masters("Stop"), 3, 1)
shpStart.AutoConnect shpStop, visAutoConnectDirNone
End Sub
Public Sub ConnectWithGlueExample()
Dim shpStart As Shape
Dim shpStop As Shape
Dim vPag As Page
Dim shpConn As Shape
Set vPag = ThisDocument.Pages(1)
Set shpStart = vPag.Drop(ThisDocument.Masters("Start"), 5, 7)
Set shpStop = vPag.Drop(ThisDocument.Masters("Stop"), 7, 5)
Set shpConn = vPag.Drop(Application.ConnectorToolDataObject, 1, 1)
Dim vFromCellStart As Cell
Dim vToCellStart As Cell
Set vFromCellStart = shpConn.CellsU("BeginX")
Set vToCellStart = shpStart.CellsSRC(visSectionObject, visRowXFormOut, 0)
Dim vFromCellStop As Cell
Dim vToCellStop As Cell
Set vFromCellStop = shpConn.CellsU("EndX")
Set vToCellStop = shpStop.CellsSRC(visSectionObject, visRowXFormOut, 0)
vFromCellStart.GlueTo vToCellStart
vFromCellStop.GlueTo vToCellStop
End Sub
如果您运行这两个,您应该得到以下结果(注意 - 没有错误检查,并假设您从中调用代码的文档中存在母版):