尝试使用VBA在PowerPoint中查找/替换

时间:2018-04-30 22:52:09

标签: vba powerpoint

我正在尝试使用VBA在powerpoint中查找/替换一些单词/短语。下面的代码卡在“Set ShpTxt = shp.TextFrame.TextRange”,错误说“指定的值超出范围”。这发生在文档的中间,而早期出现的“MONTH XX,20XX”不会被替换。请帮忙!

Sub Multi_FindReplace()

Dim sld As Slide
Dim shp As Shape
Dim ShpTxt As TextRange
Dim TmpTxt As TextRange
Dim FindList As Variant
Dim ReplaceList As Variant
Dim x As Long

FindList = Array("MONTH XX, 20XX", "United States", "Mexico")
ReplaceList = Array("September 22, 2020", "USA", "MEX")

 For Each sld In ActivePresentation.Slides 
   For Each shp In sld.Shapes
    Set ShpTxt = shp.TextFrame.TextRange

    If ShpTxt <> "" Then
      For x = LBound(FindList) To UBound(FindList)

          Set ShpTxt = shp.TextFrame.TextRange

          Set TmpTxt = ShpTxt.Replace( _
           FindWhat:=FindList(x), _
           Replacewhat:=ReplaceList(x), _
           WholeWords:=True)

          Do While Not TmpTxt Is Nothing
            Set ShpTxt = ShpTxt.Characters(TmpTxt.Start + TmpTxt.Length, 
             ShpTxt.Length)
            Set TmpTxt = ShpTxt.Replace( _
             FindWhat:=FindList(x), _
             Replacewhat:=ReplaceList(x), _
             WholeWords:=True)
          Loop

         Next x

        End If

    Next shp

  Next sld

End Sub

0 个答案:

没有答案