MS Visio中的VBA-突出显示选定形状的连接器

时间:2018-10-25 16:14:12

标签: vba visio

选择一个形状(例如正方形或更多正方形)后,所有粘贴到该形状的连接器都会突出显示红色,黄色。 以下找到的代码对我不起作用,有什么建议吗? (我不是编码员,请耐心等待)

Set shpAtEnd = cnx(1).ToSheet
' use HitTest to determine whether Begin end of connector
' is outside shpAtEnd
x = shpAtEnd.HitTest(shpTaskLink.Cells("BeginX"), _
shpTaskLink.Cells("BeginY"), 0.01)

If x = visHitOutside Then
    Selection.ShapeRange.Fill.ForeColor.SchemeColor = 2
Else
    ' do other stuff
End If

2 个答案:

答案 0 :(得分:1)

这是我对stackoverflow的第一个答案,希望下面的VBA代码可以解决有关如何在Visio中突出显示连接器或连接的形状的问题!

Public Sub HighlightConnectedShapes()

    Dim vsoShape As Visio.Shape
    Dim connectedShapeIDs() As Long
    Dim connectorIDs() As Long
    Dim intCount As Integer

    ' Highlight the selected shape
    Set vsoShape = ActiveWindow.Selection(1)
    vsoShape.CellsU("Fillforegnd").FormulaU = "RGB(146, 212, 0)"
    vsoShape.Cells("LineColor").FormulaU = "RGB(168,0,0)"
    vsoShape.Cells("LineWeight").Formula = "2.5 pt"

     ' Highlight connectors from/to the selected shape
    connectorIDs = vsoShape.GluedShapes _
      (visGluedShapesAll1D, "")
    For intCount = 0 To UBound(connectorIDs)
        ActivePage.Shapes.ItemFromID(connectorIDs(intCount)).Cells("LineColor").FormulaU = "RGB(168,0,0)"
        ActivePage.Shapes.ItemFromID(connectorIDs(intCount)).Cells("LineWeight").Formula = "2.5 pt"
    Next

    ' Highlight shapes that are connected to the selected shape
    connectedShapeIDs = vsoShape.connectedShapes(visConnectedShapesAllNodes, "")
    For intCount = 0 To UBound(connectedShapeIDs)
        ActivePage.Shapes.ItemFromID(connectedShapeIDs(intCount)).Cells("LineColor").FormulaU = "RGB(168,0,0)"
        ActivePage.Shapes.ItemFromID(connectedShapeIDs(intCount)).Cells("LineWeight").Formula = "2.5 pt"
    Next

End Sub

要运行宏,您可以考虑将其与double-click behavior个形状相关联。

如果您只需要突出显示传入/传出的连接器和传入/传出的形状,请将visGluedShapesAll1D替换为visGluedShapesIncoming1D / visGluedShapesOutgoing1D,将visConnectedShapesAllNodes替换为visConnectedShapesIncomingNodes / visConnectedShapesOutgoingNodes

visgluedshapesflagsvisconnectedshapesflags上了解更多信息。祝你好运!

答案 1 :(得分:0)

以下代码将循环所有粘贴到“选择”中第一个形状的1d形状,并将其名称写入“即时”窗口。这应该是一个很好的起点。

如果选择了Shape,Visio不会触发任何事件(至少没有一些解决方法),因此可以将宏绑定到键绑定。

visGluedShapesAll1D标志可以替换为另一个过滤器,如下所述:Microsoft Office Reference

Sub colorConnectors()

    If ActiveWindow.Selection(1) Is Nothing Then Exit Sub

    Dim selectedShape   As Shape
    Set selectedShape = ActiveWindow.Selection(1)

    Dim pg   As Page
    Set pg = ActivePage


    Dim gluedConnectorID As Variant 'variant is needed because of "For Each" Loop

    For Each gluedConnectorID In selectedShape.GluedShapes(visGluedShapesAll1D, "")
        Debug.Print pg.Shapes.ItemFromID(gluedConnectorID).NameU
    Next gluedConnectorID

End Sub