gpt4 book ai didi

excel - Excel中连接形状的VBA识别

转载 作者:行者123 更新时间:2023-12-04 21:18:41 24 4
gpt4 key购买 nike

我正在尝试在 Excel 中开发一个 VBA 解决方案,该解决方案可以识别哪些形状通过标准连接线在工作表中相互连接。
shapes example
例如,在附加的代码片段中,我需要创建一个代码来识别控制方 block 连接到两个红色圆圈(标题为风险 1 和风险 2)并在消息框中输出以下内容:“风险 1 和风险2 连接到控制”。我已经能够找到添加连接线的代码,但是我无法弄清楚如何识别连接的形状。任何指导将不胜感激!我还附上了迄今为止我能找到的代码。

Sub QuickConnect( )
Dim s1 As Shape, s2 As Shape, conn As Shape

' Create a shape
Set s1 = ActiveSheet.Shapes.AddShape(msoShapeCube, 100, 10, 50, 60)

' Create another shape
Set s2 = ActiveSheet.Shapes.AddShape(msoShapeCan, 50, 100, 50, 60)

' Create connector with arbitrary coordinates
Set conn = ActiveSheet.Shapes.AddConnector(msoConnectorCurve, 1, 1, 1, 1)

' Connect shapes
conn.ConnectorFormat.BeginConnect s1, 1
conn.ConnectorFormat.EndConnect s2, 1

' Connect via shortest path (changes connection sites)
conn.RerouteConnections
End Sub

最佳答案

因此,您需要遍历所有形状,检查它们是否是连接器(是的,连接器线也是形状)。然后您可以检查此连接线连接了哪些形状:
楼盘.ConnectorFormat.BeginConnectedShape为您提供连接线一端的形状和 .ConnectorFormat.EndConnectedShape另一端的形状。
checkout 这个:

Option Explicit

Public Sub TestConnections()
Dim shp As Variant
For Each shp In Shapes 'loop through all shapes
If shp.Connector = msoTrue Then 'check if current shape is a connector
'BeginConnectedShape is the shape on the beginning side of the connector
'EndConnectedShape is the shape on the ending side of the connector
Debug.Print shp.Name _
& " connects " & _
shp.ConnectorFormat.BeginConnectedShape.Name _
& " with " & _
shp.ConnectorFormat.EndConnectedShape.Name
End If
Next shp
End Sub
对于以下形状
enter image description here
它输出
Curved Connector 3 connects Cube 1 with Can 2
Curved Connector 6 connects Cube 5 with Can 2

关于excel - Excel中连接形状的VBA识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63841955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com