在PowerPoint中查找文本并使用VBA替换excel中单元格中的文本,但不断获得运行时错误'-2147024809(80070057)'

问题描述 投票:2回答:1

我正在尝试使用Excel文件中的单元格中的值查找并替换PowerPoint幻灯片中的单词列表。我在PowerPoint上运行VBA并且它一直给出这个错误

运行时错误'-2147024809(80070057)':指定的值超出范围。

代码似乎停在这一行(第一行):

Set ShpTxt = shp.TextFrame.TextRange

请帮忙!

我一直在浏览具有类似目的和错误的其他帖子,并尝试了大约20种不同的组合,从互联网和我的想法,但没有工作,没有得到确凿的线索。

Sub MergePPT3()

    Dim pp As Object
    Dim pptemplate As Object
    'Dim headerbox As TextRange
    'Dim contextbox As TextRange
    Dim x As Long
    Dim y As Long
    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 ExApp As Object
    Dim ExInput As Object

    Dim SuName As String
    Dim WFWS As String
    Dim WFYOY As String
    Dim CGWS As String
    Dim CGYOY As String
    Dim RNKG As String
    Dim MKTCAT As String

    Set ExApp = GetObject(, "Excel.Application")
    ExApp.Visible = True
    Set ExInput = ExApp.Workbooks.Open(ActivePresentation.Path & "/Testing.xlsm")

    y = 2

    SuName = ExInput.Sheets("SuIDs").Range("B" & y).Value
    WFWS = ExInput.Sheets("SuIDs").Range("C" & y).Value
    WFYOY = ExInput.Sheets("SuIDs").Range("D" & y).Value
    CGWS = ExInput.Sheets("SuIDs").Range("E" & y).Value
    CGYOY = ExInput.Sheets("SuIDs").Range("F" & y).Value
    RNKG = ExInput.Sheets("SuIDs").Range("G" & y).Value
    MKTCAT = ExInput.Sheets("SuIDs").Range("H" & y).Value

    FindList = Array("SUNAME", "WFWS", "WFYOY", "CGWS", "CGYOY", "RNKG", "MKTCAT")
    ReplaceList = Array(SuName, WFWS, WFYOY, CGWS, CGYOY, RNKG, MKTCAT)

     For Each sld In ActivePresentation.Slides

        For Each shp In sld.Shapes
          'Store shape text into a variable
            Set ShpTxt = shp.TextFrame.TextRange

          'Ensure There is Text To Search Through
            If ShpTxt <> "" Then
              For x = LBound(FindList) To UBound(FindList)

                'Store text into a variable
                 Set ShpTxt = shp.TextFrame.TextRange

                'Find First Instance of "Find" word (if exists)
                 Set TmpTxt = ShpTxt.Replace( _
                   FindWhat:=FindList(x), _
                   Replacewhat:=ReplaceList(x), _
                   WholeWords:=True)

                'Find Any Additional instances of "Find" word (if exists)
                  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

我希望代码能够使用Excel文件中的输入成功搜索并替换PPT文件中列出的所有单词。在代码中,我使用变量“y”作为循环Excel代码中多行输入的代码的可能性。

excel vba replace runtime-error powerpoint
1个回答
1
投票

并非所有形状都有TextFrame

从文档:

在应用HasTextFrame属性之前,使用TextFrame属性确定形状是否包含文本框架。

所以尝试:

If shp.HasTextFrame
    Set ShpTxt = shp.TextFrame.TextRange
End If
© www.soinside.com 2019 - 2024. All rights reserved.