Visio VBA - 如何分配具有已知固定距离的形状

问题描述 投票:1回答:1

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

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”窗口,我可以看到数组没有被填充。

更新:

为什么这样做,

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

问候,斯科特

arrays vba shape visio visio-vba
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.