我想通过VBA将形状粘贴到另一个形状。所有形状都是使用UserForm模块创建的。我希望某些形状与箭头相连(它也会通过…放在页面上)
啊,@ Surrogate打败了我:)但是因为我已经开始写了...除了他的答案,这很好地展示了如何调整内置的动态连接器这里是你的团队寻找方法+自定义连接器。
代码假定了一些事情:
Public Sub TestConnect() Dim shp As Visio.Shape 'connector Dim src As Visio.Shape 'connect this Dim aim As Visio.Shape 'to this Dim vPag As Visio.Page Set vPag = ActivePage Set shp = vPag.Drop(ActiveDocument.Masters("MyConn"), 1, 1) shp.CellsU("ObjType").FormulaU = 2 Set src = vPag.Shapes(1) Set aim = getShape(7, "Prop.ID") If Not aim Is Nothing Then shp.CellsU("BeginX").GlueTo src.CellsU("PinX") shp.CellsU("EndX").GlueTo aim.CellsU("PinX") End If End Sub Function getShape(id As Integer, propName As String) As Shape Dim shp As Shape Dim subshp As Shape For Each shp In ActivePage.Shapes If shp.Type = 2 Then For Each subshp In shp.Shapes If subshp.CellExistsU(propName, 0) Then If subshp.CellsU(propName).ResultIU = id Then Set getShape = subshp Exit For End If End If Next subshp End If If shp.CellExistsU(propName, 0) Then If shp.CellsU(propName).ResultIU = id Then Set getShape = shp Exit For End If End If Next End Function
请注意,如果你 阅读文档 对于 Cell.GlueTo ,你会看到这个项目:
Cell.GlueTo
2-D形状的针脚(创建动态胶水): 的 形状被胶合 从必须是可路由的(ObjType包括visLOFlagsRoutable) 强> 或者有一个 动态胶水类型(GlueType包括visGlueTypeWalking),并且确实如此 不禁止动态胶水(GlueType不包括 visGlueTypeNoWalking)。胶合到PinX会产生动态胶水 水平行走偏好和胶合到PinY创建动态胶水 具有垂直行走偏好。
因此我为什么要设置 ObjType 细胞到2( VisCellVals.visLOFlagsRoutable )。通常你会在主实例中设置它,因此不需要那行代码。
ObjType
VisCellVals.visLOFlagsRoutable
请试试这段代码
Dim connector As Shape, src As Shape, aim As Shape ' add new connector (right-angle) to page Set connector = Application.ActiveWindow.Page.Drop(Application.ConnectorToolDataObject, 0, 0) ' change Right-angle Connector to Curved Connector connector.CellsSRC(visSectionObject, visRowShapeLayout, visSLOLineRouteExt).FormulaU = "2" connector.CellsSRC(visSectionObject, visRowShapeLayout, visSLORouteStyle).FormulaU = "1" Set src = Application.ActiveWindow.Page.Shapes.ItemFromID(4) Set aim = Application.ActiveWindow.Page.Shapes.ItemFromID(2) Dim vsoCell1 As Visio.Cell Dim vsoCell2 As Visio.Cell Set vsoCell1 = connector.CellsU("BeginX") Set vsoCell2 = src.Cells("PinX") vsoCell1.GlueTo vsoCell2 Set vsoCell1 = connector.CellsU("EndX") Set vsoCell2 = aim.Cells("PinX") vsoCell1.GlueTo vsoCell2