【发布时间】:2019-05-21 18:55:49
【问题描述】:
我是 VBA 新手,这是我的第一个任务,涉及预先存在的 Visio 绘图。
Visio 绘图由几个形状组成,我最终想要一种方法来使用 vba 代码检测哪些形状是电缆(由动态连接器连接的两个“连接器”形状)。去做这个, 1) 我想首先将所有形状名称存储在一个数组中。 2)然后,我想用已知的连接器形状名称交叉检查该数组,并创建一个仅包含这些连接器形状的新数组。 3)接下来,我将检查每个连接器形状连接到什么,这将允许我确定它是什么类型的电缆(我已经完成了这部分代码)。 4) 最后,我将电缆的# 分配给连接器形状之一(我想我也有这方面的工作代码)。
我正在尝试弄清楚如何使用现有代码实现步骤 1 和 2。
目前我只能在选择其中一种形状时检测连接的形状:
Public Sub ConnectedShapes()
' Get the shapes that are at the other end of
' incoming connections to a selected shape
Dim vsoShape As Visio.Shape
Dim allShapes As Visio.Shapes
Dim lngShapeIDs() As Long
Dim intCount As Integer
If ActiveWindow.Selection.Count = 0 Then
MsgBox ("Please select a shape that has connections.")
Exit Sub
Else
Set vsoShape = ActiveWindow.Selection(1)
End If
Set allShapes = ActiveDocument.Pages.Item(1).Shapes
lngShapeIDs = vsoShape.ConnectedShapes(visConnectedShapesAllNodes, "")
Debug.Print " Shape selected: ";
Debug.Print vsoShape
Debug.Print " Shape(s) connected: ";
For intCount = 0 To UBound(lngShapeIDs)
connectedItem = allShapes.ItemFromID(lngShapeIDs(intCount)).Name
Debug.Print connectedItem
If InStr(1, vsoShape, "USB A - top") = 1 Then
If InStr(1, connectedItem, "USB A Female") = 1 Then
' write cable's number
ElseIf InStr(1, connectedItem, "USB Mini B") = 1 Then
' write cable's number
ElseIf InStr(1, connectedItem, "USB Micro B") = 1 Then
' write cable's number
ElseIf InStr(1, connectedItem, "USB C Male") = 1 Then
' write cable's number
End If
End If
Next
End Sub
是否有 Visio vba 的内置函数可以帮助我实现步骤 1 和 2?在文档中查找所有形状并将它们存储在数组中的最简单方法是什么?
【问题讨论】: