You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visio VBA调用ConnectedShapes报错:Invalid Sheet Identifier

解决Visio VBA中ConnectedShapes返回ID无法通过ActivePage.Shapes访问的问题

首先,你的问题根源在于**ConnectedShapes返回的是全局唯一的ShapeID,而ActivePage.Shapes只能访问当前活动页上的形状**。如果返回的ShapeID对应的形状不在当前页,直接用ActivePage.Shapes(ID)就会触发「Invalid Sheet Identifier」错误——因为当前页的Shapes集合里根本找不到这个ID的形状。

解决方案:使用全局形状集合通过ID获取形状

Visio提供了ActiveDocument.Shapes.ItemFromID()方法,它可以在整个文档范围内根据ShapeID查找形状,不管该形状在哪个页面。修改你的代码如下:

Public Sub ConnectedShapes_Outgoing_Example()
' Get the shapes that are connected to the selected shape
' by outgoing connectors.
    Dim vsoShape As Visio.Shape
    Dim lngShapeIDs() As Long
    Dim intCount As Integer
    Dim targetShape As Visio.Shape

    If ActiveWindow.Selection.Count = 0 Then
        MsgBox "Please select a shape that has connections"
        Exit Sub
    Else
        Set vsoShape = ActiveWindow.Selection(1)
    End If

    lngShapeIDs = vsoShape.ConnectedShapes _
      (visConnectedShapesOutgoingNodes, "")
    Debug.Print "Shapes at the end of outgoing connectors:"
    For intCount = 0 To UBound(lngShapeIDs)
        ' 使用全局文档的形状集合获取目标形状
        Set targetShape = ActiveDocument.Shapes.ItemFromID(lngShapeIDs(intCount))
        Debug.Print "Name: " & targetShape.Name & ", Page: " & targetShape.Page.Name
    Next
End Sub

额外说明

  • 如果你的场景中只需要当前页的 outgoing连接形状,可以在调用ConnectedShapes时添加筛选条件,限制只返回当前页的形状:
    lngShapeIDs = vsoShape.ConnectedShapes(visConnectedShapesOutgoingNodes, "PageID=" & ActivePage.ID)
    
  • 「Invalid Sheet Identifier」错误本质是尝试访问一个不存在于当前集合(ActivePage.Shapes)中的元素,所以确保你访问的集合包含目标形状是关键。

内容的提问来源于stack exchange,提问作者smackenzie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 16:45:29