Visio VBA 从选择中获取形状

Visio VBA Get Shape from Selection

我需要在 Visio 中引用一个 selected(通过鼠标单击,它只需要是一个 selection)形状。但是形状可能在一个组中。 当我 select 组中的一个形状时,我似乎无法让它工作,shp 对象仍然是空的。

Sub selectTest()
    Dim sel As Visio.Selection
    Set sel = ActiveWindow.Selection
    Dim shp As Visio.Shape
    Set shp = sel.PrimaryItem

    If Not shp Is Nothing Then
        MsgBox "It worked"
    Else
        MsgBox "No shape in sel"
    End If        
End Sub

当 "Top-Level" 组被 select 编辑时,它起作用了。 当组内的形状(也可能是组本身)被 selected 时,它不会。 当不在组中的形状被 selected 时,它再次起作用。

上下文:我想从上下文菜单中触发自定义 VBA 代码。当您右键单击该形状时,它会自动 selected。

如何获取组中形状的引用?

编辑:进一步澄清:我的文档中的形状都有相应的数据库条目。我想(通过 XML)将自定义删除按钮添加到上下文菜单(有效),这应该调用一个删除方法,该方法获取调用该方法的形状作为参数,以便它可以搜索相应的数据库条目并删除它(以及任何子形状的条目,如果 selected 形状是一个组)在形状(及其所有子形状)被删除之前 shape.delete

我不懂 Visio VBA,但请尝试一下:

更新

Sub selectTest()

    Dim x As Integer
    Dim sel As Visio.Selection
    Dim shp As Visio.Shape
    Dim inner_shape As Visio.Shape

    Set sel = ActiveWindow.Selection
    Set shp = sel.PrimaryItem

    For x = 1 To shp.Shapes.Count
        Set inner_shape = shp.Shapes(x)
        '// Do something with inner shape
    Next

End Sub

使用Selection.IterationMode 属性在选择中包含子选择的形状

Set sel = ActiveWindow.Selection
sel.IterationMode = 0
Set shp = sel.PrimaryItem