Visio VBA - 如何以已知的固定距离分布形状

Visio VBA - How can I distribute shapes with a known, fixed distance

我想将所有当前选定的形状放入一个数组中。然后我想对该数组进行排序,以便我可以找到数组中最顶部或最左侧的形状。然后我想使用那个形状作为我的起点,然后从那里将其他形状对齐一个固定的、已知的距离。我试过将形状放入数组中,如下所示:

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

我尝试创建没有类型规范的数组,指定为变体,并在此示例中指定为选择。我也不知道我是否可以将它们放入某种列表中?显然,在我可以填充数组之前,我无法对数组进行排序然后分配我的形状。我在代码中放置了一个断点,我打开了 "Locals" window,我可以看到数组没有被填充。

更新:

为什么这样做,

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

此致, 斯科特

您的代码中存在几个问题 - 仅修复一个问题不会让您进一步了解是否确实修复了任何问题。

  • 您的 arrShapes 被声明为一般对象 - Selection 对象是万事通的对象之一,并且 none 的大师。
  • 你在给数组赋值时没有"Set"。

我这台机器上没有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