擦除后实例化动态数组
Instantiating a dynamic array after erase
我正在尝试在擦除动态数组后对其进行实例化。
我使用一个数组来存储我创建的形状,这样我就不需要循环页面上的每个形状。因为已经有很多了。每个新页面我都会擦除数组,但是当我尝试将第一个形状添加到数组时 运行 会出现 "subscript out of range" 错误。
Dim SeqShapes() As Shape
For PageCount = 0 to activeDocument.Pages.Count
Erase SeqShapes
For ShapesNeeded = 0 to ShapesCount
Set NewShape = ActivePage.Drop(SomeShape, 20, 20)
SeqShapes(UBound(SeqShapes)) = NewShape
Next
'Some more code
Next
这 returns 一个错误,因为数组中没有条目。
我不想使用固定数组,因为无法事先知道要创建多少个形状。
我试过添加虚拟记录,但似乎无法理解语法:
Dim SeqShapes() As Shape
Dim DummyShape As Shape
For PageCount = 0 to activeDocument.Pages.Count
Erase SeqShapes
SeqShapes(0) = DummyShape
For ShapesNeeded = 0 to ShapesCount
Set NewShape = ActivePage.Drop(SomeShape, 20, 20)
SeqShapes(UBound(SeqShapes)) = NewShape
Next
'Some more code
Next
如有任何帮助,我们将不胜感激。
使用集合而不是数组
Dim SeqShapes As Collection
For PageCount = 0 to activeDocument.Pages.Count
Set SeqShapes = Nothing ' Easiest way to clear it is to recreate it.
Set SeqShapes = New Collection
Dim ShapesNeeded
Dim newShape As Shape
For ShapesNeeded = 0 To 3
Set newShape = ActivePage.Drop(SomeShape, 20, 20)
SeqShapes.Add newShape ' Add the shape into Collection
Next ShapesNeeded
...
Next PageCount
遍历集合中的所有形状:
' Using ForEach (you have to declare you running variable as Variant)
Dim sh As Variant
For Each sh In SeqShapes
Debug.Print sh.Name
Next sh
' Using for
Dim i As Long
For i = 1 To SeqShapes.Count
Debug.Print SeqShapes(i).Name
Next i
我正在尝试在擦除动态数组后对其进行实例化。 我使用一个数组来存储我创建的形状,这样我就不需要循环页面上的每个形状。因为已经有很多了。每个新页面我都会擦除数组,但是当我尝试将第一个形状添加到数组时 运行 会出现 "subscript out of range" 错误。
Dim SeqShapes() As Shape
For PageCount = 0 to activeDocument.Pages.Count
Erase SeqShapes
For ShapesNeeded = 0 to ShapesCount
Set NewShape = ActivePage.Drop(SomeShape, 20, 20)
SeqShapes(UBound(SeqShapes)) = NewShape
Next
'Some more code
Next
这 returns 一个错误,因为数组中没有条目。 我不想使用固定数组,因为无法事先知道要创建多少个形状。
我试过添加虚拟记录,但似乎无法理解语法:
Dim SeqShapes() As Shape
Dim DummyShape As Shape
For PageCount = 0 to activeDocument.Pages.Count
Erase SeqShapes
SeqShapes(0) = DummyShape
For ShapesNeeded = 0 to ShapesCount
Set NewShape = ActivePage.Drop(SomeShape, 20, 20)
SeqShapes(UBound(SeqShapes)) = NewShape
Next
'Some more code
Next
如有任何帮助,我们将不胜感激。
使用集合而不是数组
Dim SeqShapes As Collection
For PageCount = 0 to activeDocument.Pages.Count
Set SeqShapes = Nothing ' Easiest way to clear it is to recreate it.
Set SeqShapes = New Collection
Dim ShapesNeeded
Dim newShape As Shape
For ShapesNeeded = 0 To 3
Set newShape = ActivePage.Drop(SomeShape, 20, 20)
SeqShapes.Add newShape ' Add the shape into Collection
Next ShapesNeeded
...
Next PageCount
遍历集合中的所有形状:
' Using ForEach (you have to declare you running variable as Variant)
Dim sh As Variant
For Each sh In SeqShapes
Debug.Print sh.Name
Next sh
' Using for
Dim i As Long
For i = 1 To SeqShapes.Count
Debug.Print SeqShapes(i).Name
Next i