2016-10-21 153 views
0

我有一个非常长的PPT演示文稿(大约850张幻灯片),后半部分充满了我想删除某些文本的形状。可悲的是,它似乎与Slide Master无关,所以我不能使用它。VBA Powerpoint:使用特定文本删除形状。运行时错误'-2147024809(80070057)':指定的值超出范围

我得到了一个错误:

Run-time error '-2147024809 (80070057)': 
The specified value is out of range 

下面的代码,我此刻

Sub DeleteShapeWithSpecTxt() 


Dim oSl As Slides, oSh As Shapes, oTr As TextRange 
Dim str As String 
Dim testcomp1, testcomp2 
Dim lppt, ShapeNb, k, j As Long 
Dim pptAct 
Set pptAct = PowerPoint.ActivePresentation 


str = pptAct.Slides(335).Shapes(4).TextFrame.TextRange.Text 
lppt = pptAct.Slides.Count 




For k = 1 To lppt 
    ShapeNb = pptAct.Slides(k).Shapes.Count 
    For j = 1 To ShapeNb 
     If pptAct.Slides(k).Shapes(j).HasTextFrame And StrComp(str, pptAct.Slides(k).Shapes(j).TextFrame.TextRange.Text) = 0 Then 
      pptAct.Slides(k).Shapes(j).Delete 
     End If 
    Next 
Next 

末次

回答

0

了有几个原因,该代码会产生一个错误。首先,如果幻灯片335或形状4不存在(尝试使这些数字变为动态或处理错误)。接下来,你的If行将评估这两个部分,所以如果该形状没有TextFrame,VBA仍然会尝试评估第二部分,从而引发错误。最后,还需要在任何可能删除对象的对象集合中向后计数。你也可以简化这个使用对于每个下构建和任选的主代码通过搜索文本的程序:

Sub DeleteShapeWithSpecTxt(Optional sSearch As String) 
    Dim oSld As Slide 
    Dim oShp As Shape 
    Dim lShp As Long 

    On Error GoTo errorhandler 
    If sSearch = "" Then sSearch = ActivePresentation.Slides(335).Shapes(4).TextFrame.TextRange.Text 

    For Each oSld In ActivePresentation.Slides 
    ' I would usually use the next line to loop through all shapes on the slide but can't in this case as shapes may be deleted 
    'For Each oShp In oSld.Shapes 
    For lShp = oSld.Shapes.Count To 1 Step -1 
     With oSld.Shapes(lShp) 
     If .HasTextFrame Then 
      If StrComp(sSearch, .TextFrame.TextRange.Text) = 0 Then .Delete 
     End If 
     End With 
    Next 
    Next 
Exit Sub 
errorhandler: 
    Debug.Print "Error in DeleteShapeWithSpecTxt : " & Err & ": " & Err.Description 
    On Error GoTo 0 
End Sub 

如果你想搜索文本的动态,这是一个很好的简单方法。只需更换如果SSEARCH =“” ...符合这样的:

If sSearch = "" Then sSearch = InputBox("Enter test to search for and all shapes matching the text will be deleted across this presentation:","Delete Matching Shapes","test") 
0

@JamieG谢谢,我发现了同样的解决方案(但不是整齐的代码)。我要发布它,当我看到你的答案

干杯

编辑:更精确:字符串的动态设置是一种很难(我VBA的知识不是很先进)。出于这个原因,对于我来说,选择某个幻灯片/形状中的文本要容易得多。 对IF的评论是关于点的,以及删除时的向后计数

+0

很酷。我更新了答案,以包含在宏运行时从用户获取搜索字符串的可能性。如果答案有帮助,可以自由地为它投票:-) –

+0

会很喜欢,但我需要15点的声望,我没有,因为我是新的stackoverflow ... –

相关问题