Powerpoint VBA:循环使文本框可见会在添加到前面时出现问题

时间:2014-06-04 13:25:40

标签: vba loops powerpoint

所以我写了下面的代码,使我的演示文稿中的文本框变得可见,然后将它们带到前面(它们被一个单独的宏看不见):

Dim oSld As Slide
Dim oShp As Shape
Dim oPPT As Presentation

For Each oSld In ActivePresentation.Slides
For Each oShp In oSld.Shapes

If oShp.Type = msoTextBox Then
        oShp.Visible = True
        oShp.ZOrder msoBringToFront
End If

Next oShp
Next oSld

在我添加前置命令之前,它完美运行!现在,只有大约一半的形状在运行代码时可见。我一直在网上阅读,看来ZOrder改变了形状的数量,这就是为什么只有一些可见......但是无法解决它的问题!真的很感激一些帮助!

2 个答案:

答案 0 :(得分:0)

你已经指出了原因。 For Each / Next循环似乎在其开始时拍摄形状顺序的快照。如果您更改形状顺序或删除循环体中的形状,它会抛出一些东西。相反,尝试下面的(未经测试的)(空气)代码来构建一个对形状的引用数组,然后从数组中一次处理一个:

Dim aShapeArray() as Shape
Dim x as Long
ReDim aShapeArray(1 to oSld.Shapes.Count) as Shape

' Build an array of shapes
For Each oShp In oSld.Shapes
  Set aShapeArray(x) = oShp
  x = x+1
Next

' Then do what you need to do with each shape in the array
For x = 1 to Ubound(aShapeArray)
Set oShp = aShapeArray(x)
If oShp.Type = msoTextBox Then
        oShp.Visible = True
        oShp.ZOrder msoBringToFront
End If
Next

答案 1 :(得分:0)

感谢您对@SteveRindsberg的所有帮助,对您的代码进行了一些调整,我破解了它:)

Dim oSld As Slide
Dim aShapeArray() 
Dim x As Long


For Each oSld In ActivePresentation.Slides
x = 1
If oSld.Shapes.Count > 0 Then
ReDim aShapeArray(1 To oSld.Shapes.Count)

' Build an array of shapes
For Each oShp In oSld.Shapes
  Set aShapeArray(x) = oShp
  x = x + 1
Next


' Then do what you need to do with each shape in the array
For x = 1 To UBound(aShapeArray)
Set oShp = aShapeArray(x)
      If oShp.Type = msoTextBox Then
        oShp.Visible = True
        oShp.ZOrder msoBringToFront
      End If
Next
End If

Next