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
相关产品推荐
相关产品推荐

