vba - 如何在 Visio 绘图中查找所有形状并将每个形状添加到数组中?
问题描述
我是 VBA 新手,这是我的第一个任务,涉及预先存在的 Visio 绘图。
Visio 绘图由几个形状组成,我最终想要一种方法来使用 vba 代码检测哪些形状是电缆(由动态连接器连接的两个“连接器”形状)。为此,1) 我想首先将所有形状名称存储在一个数组中。2)然后,我想用已知的连接器形状名称交叉检查该数组,并创建一个仅包含这些连接器形状的新数组。3)接下来,我将检查每个连接器形状连接到什么,这将允许我确定它是什么类型的电缆(我已经完成了这部分代码)。4) 最后,我将电缆的# 分配给连接器形状之一(我想我也有这方面的工作代码)。
我试图弄清楚如何用我现有的代码实现步骤 1 和 2。
Currently I am only able to detect connected shapes when one of those shapes is selected:
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?在文档中查找所有形状并将它们存储在数组中的最简单方法是什么?
解决方案
了解您想要的业务逻辑是第一步。您的步骤 1 和 2 可以是一个步骤。
了解您的解决方案空间就是了解编程语言为您提供的工具范围。在这种情况下,它是关于如何有效地循环(例如For Each
)和信息容器(例如Collection
)。
这是一些示例代码:
Option Explicit ' Always use this at the top of a module. Always.
Function ExampleFindShapes(chosenPage as Page) as Collection
Dim foundShapes as New Collection ' Note the new part, this initialised the Collection
Dim shapeLoopIterator as Shape
Dim arrayLoopIterator as Long
Dim validShapes as Variant
validShapes = Array("Bob", "Harry", "George")
For each shapeLoopIterator in chosenPage.Shapes ' One way to loop through an object collection
For arrayLoopIterator = LBound(validShapes) to UBound(validShapes) ' One way to loop through an array
If shapeLoopIterator.Name = validShapes(arrayLoopIterator) Then
foundShapes.Add shapeLoopIterator ' store the found shape as a reference to the shape
'Could put something in here to break out of the loop
End If
Next arrayLoopIterator
Next shapeLoopIterator
ExampleFindShapes = foundShapes
End Function
从内存编码,因为我没有在这台机器上安装 Visio,所以Page
可能是别的东西。
我已经存储了对形状的引用而不仅仅是名称,因为找到的形状的集合将更容易在您的第 3 部分和第 4 部分中使用,而不是您必须再次查找和引用形状。
如果您使用分组形状,答案会变得有点复杂。如果是这种情况,我建议使用一个新问题来引用这个问题,因为答案将涉及递归并将集合向下传递,这会稍微复杂一些。
推荐阅读
- rust - 在使用关联类型的情况下,如何为通用容器实现 Borrow?
- c# - OntriggerEnter 未调用 playerController
- javascript - 如何从字符串制作 HTML 元素,然后在 React 中选择一个元素?
- css - 如何在 React 中隐藏包装的组件
- cassandra - 无法强制转换为格式化日期 - Cassandra 时间戳类型
- laravel - laravel 数据库种子数据透视表
- java - 处理3中二维数组的变化值
- ios - 在 UIViewControllerTransitioning 如何在转换进行时点击视图内的按钮?
- javascript - 从索引视图中的部分视图重新渲染索引视图?
- java - 如何通过 Android HAL 创建环回摄像头?