我想将所有当前选定的形状放入数组中。然后,我想对该数组进行排序,以便找到数组中最顶部或最左侧的形状。然后我喜欢用这个形状作为我的起点,然后从那里将其他形状与固定的已知距离对齐。我试图将形状放入这样的数组中:
Dim numShapes As Integer, i As Integer
Dim arrShapes As Visio.Selection
numShapes = Visio.ActiveWindow.Selection.Count
For i = 1 To numShapes
arrShapes(i) = Visio.ActiveWindow.Selection(i)
Next i
我尝试创建没有类型规范的数组,指定为variant,并在此示例中指定为选择。我不知道我是否可以将它们列入某种列表中?显然,我无法对数组进行排序,然后分配我的形状,直到我可以填充数组。我在代码中设置了一个断点,我有"当地人"窗口打开,我可以看到数组没有被填充。
更新
为什么这样做,
Dim Sel As Visio.Selection
Dim Shp As Visio.Shape
Set Sel = Visio.ActiveWindow.Selection
For Each Shp in Sel
Debug.Print Shp.Name
Next
这不是吗?
Dim i As Integer
Dim Shp As Visio.Shape
For i = 1 To Visio.ActiveWindow.Selection.Count
Set Shp = Visio.ActiveWindow.Selection(i)
Debug.Print Shp.Name
Next i
此致 斯科特
答案 0 :(得分:2)
您的代码中存在一些问题 - 如果您确实修复了任何问题,那么只有一个问题不会让您进一步理解。
arrShapes
被声明为一般对象 - 选择
对象是所有交易中杰克的对象之一
无人掌握。 我没有在这台机器上安装Visio,因此无法直接测试下面的代码。我还假设所有选择的项目都是形状(通常是Visio中的安全假设)。
Dim numShapes As Integer, i As Integer
Dim arrShapes() As Shape ' Set this up as an array of shape
If Visio.ActiveWindow.Selection.Count > 0 then ' don't want to cause a problem by setting the array to 0!
ReDim arrShapes(Visio.ActiveWindow.Selection.Count)
numShapes = Visio.ActiveWindow.Selection.Count ' while not really necessary it does help explain the code.
For i = 1 To numShapes
' must Set as we want the reference to the shape, not the default value of the shape.
Set arrShapes(i) = Visio.ActiveWindow.Selection(i)
Next i
Else
MsgBox "No shapes selected. Nothing done." ' soft fail
End If