2013-08-29 30 views
0

我的宏只在特定的一组形状上通过ActiveWindow.Selection.shapeRange时发出“索引超出范围”错误。Powerpoint VBA中的索引超出范围错误

具体呈现可以在http://free-editable-worldmap-for-powerpoint.en.softonic.com找到(选择任何较大的组的形状,即南美,并运行该代码的复制错误)

的代码如下:

Dim shp As Shape 
For Each shp In ActiveWindow.Selection.shapeRange 
    shp.Fill.Transparency = 0 'Or any other code 
Next shp 

我还尝试使用For循环没有成功('For i = 1到ActiveWindow.Selection.shapeRange.Count步骤1')。值得注意的是,没有特定的索引来引发错误 - 有时它是i = 3,有时i = 35,有时更多。

+0

你能更具体吗?我无法复制那个错误,实际上,'For Each'迭代应该总是*规避'索引超出范围'的错误。 **哪一组形状导致问题**? –

回答

0

此幻灯片中有一些msoLine形状(shp.Type = 9)。这会引发错误:

The specified value is out of range.

如果您无意中选择了它们。 (我希望Object does not support this property or method错误,因为msoLine没有.Fill成员 - 但有时错误信息是神秘的。无论如何,这个错误可能被困在)。

该幻灯片上的所有其他形状都是类型5或6,它们支持.Fill

我用它来调试,虽然我无法复制你的具体错误(除非你错误地输入错误描述),也许它会对你有所帮助。它尝试在幻灯片中设置Fill.Forecolor所有形状,并将在VBE中的一些窗口中打印有关错误的信息。

Sub Test() 
'Determine there are shapes which do not have a .Fill.ForeColor 
Dim shp As Shape 
For Each shp In ActivePresentation.Slides(1).Shapes 
    On Error Resume Next 
    shp.Fill.ForeColor.RGB = 43506 
    If Err.Number <> 0 Then 
     Debug.Print Err.Description & " >> " & _ 
      shp.Name & " is shape type " & shp.Type 
     On Error GoTo 0 
    Else: 
     shp.Fill.Transparency = 0 
     shp.Fill.ForeColor.RGB = 12632256 
    End If 
Next 

End Sub