在visio中连接两个2-d形状

时间:2012-08-03 07:42:20

标签: vba visio

我正在尝试做最简单的事情 - 使用GlueTo宏在visio中连接两个2d形状。 我删掉了两个形状,用“In”和“Out”添加了连接点。 现在,当我尝试用鼠标连接它们时 - 一切都好。 然后我录制了一个宏:

Sub Macro1()

'Enable diagram services
Dim DiagramServices As Integer
DiagramServices = ActiveDocument.DiagramServicesEnabled
ActiveDocument.DiagramServicesEnabled = visServiceVersion140

Dim UndoScopeID1 As Long
UndoScopeID1 = Application.BeginUndoScope("1")
ActiveWindow.DeselectAll
ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemFromID(1), visSelect
Application.ActiveWindow.Selection.Move -1.161417, 0.669291
Dim vsoCell1 As Visio.Cell
Dim vsoCell2 As Visio.Cell
Set vsoCell1 = Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsU("PinX")
Set vsoCell2 = Application.ActiveWindow.Page.Shapes.ItemFromID(15).CellsSRC(7, 0, 0)
vsoCell1.GlueTo vsoCell2
Dim vsoCell3 As Cell
Dim vsoCell4 As Cell
Set vsoCell3 = Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(visSectionConnectionPts, 0, 0)
Set vsoCell4 = Application.ActiveWindow.Page.Shapes.ItemFromID(15).CellsSRC(visSectionConnectionPts, 0, 0)
vsoCell3.GlueTo vsoCell4
Application.EndUndoScope UndoScopeID1, True

'Restore diagram services
ActiveDocument.DiagramServicesEnabled = DiagramServices

End Sub

它不起作用......如果我解开那些形状并运行录制的宏,它将抛出“此操作的不适当的源对象”

我该如何解决?

1 个答案:

答案 0 :(得分:2)

录制的代码非常特定于录制宏时的绘图状态。编辑图纸时会很快破坏。例如,记录的代码假定页面上已有一维形状,并且页面形状集合中的ID为15。记录的宏中有许多其他非常脆弱的假设。

我建议更好的方法是学习如何自己编写宏,因为你知道功能要求和你可以对绘图状态做出的假设。请参阅“开发Visio解决方案”一书中的这一部分,了解如何编写代码以将形状连接在一起:Creating a Connected Drawing from a Program