替换程序崩溃

时间:2018-09-04 12:59:27

标签: vba powerpoint powerpoint-vba

我对这段代码有疑问,应该遍历所有幻灯片中的所有形状并替换它们,但是它只是在某个时刻崩溃了。首先,它告诉您插入要搜索的词,然后插入要替换的词。一旦插入,它就会崩溃,有时会用几句话替换掉它们,而其他则不会。有人知道问题出在哪里吗?代码如下:

Sub Reemplazar()
Dim sld As Slide
Dim shp As Shape
Dim ShpTxt As TextRange
Dim TmpTxt As TextRange
Dim Findword As String
Dim ReplaceWord As String
Findword = InputBox("Ingrese palabra que se quiere reemplazar")
ReplaceWord = InputBox("Ingrese palabra por la que se va a reemplazar")
  For Each sld In ActivePresentation.Slides
    For Each shp In sld.Shapes
        Set ShpTxt = shp.TextFrame.TextRange
        If ShpTxt <> "" Then
            Set ShpTxt = shp.TextFrame.TextRange
            Set TmpTxt = ShpTxt.Replace( _
            FindWhat:=Findword, _
            Replacewhat:=ReplaceWord, _
            WholeWords:=False)
            Do While Not TmpTxt Is Nothing
              Set ShpTxt = ShpTxt.Characters(TmpTxt.Start + TmpTxt.Length, ShpTxt.Length)
              Set TmpTxt = ShpTxt.Replace( _
              FindWhat:=Findword, _
              Replacewhat:=ReplaceWord, _
              WholeWords:=False)
            Loop
        End If
    Next shp
  Next sld
End Sub

程序停止并要求调试,给出消息“错误'-2147024809(80070057)”,并突出显示“ Set ShpTxt = shp.TextFrame.TextRange”行

1 个答案:

答案 0 :(得分:4)

在尝试进行替换之前,请确认其形状可以容纳文本,如果可以,则可以容纳文本:

If osh.HasTextFrame Then
    If osh.TextFrame.HasText Then
        ' NOW do your replacements
    End If
End If