VBA 在 Visio 中计算形状

VBA Counting Shapes in Visio

我想数一数我的 sheet 上有多少种形状并将其保存到标签中。

Sub Counter()

Dim shp As Visio.Shape
Dim i As Integer

For Each shp In ActivePage.Shapes
    If shp.Master.Name Like "DV-ED.*" Then
       i = i + 1
        End If
        ActivePage.Shapes("SheetED").Characters.Text = CStr(i)
Next shp
End Sub

问题是,在该类型的最后一个形状之后我得到一个错误

"Run Time Error '91' : Object Variable or With Block not Set"

我不是很熟悉VBA

在访问对象的 Name 等属性之前,请确保已设置该对象:

Sub Counter()

Dim shp As Visio.Shape
Dim i As Integer

For Each shp In ActivePage.Shapes
   If shp.Master<>Nothing Then
      If shp.Master.Name Like "DV-ED.*" Then
        i = i + 1
      End If
      ActivePage.Shapes("SheetED").Characters.Text = CStr(i)
   End If
Next shp
End Sub