希望在 powerpoint 中查找文本并使用 excel 中单元格中的文本替换为 vba,但不断出现错误

Looking to find text in powerpoint and replace using text from a cell in excel using vba, but keep getting error

我的这部分代码应该在 powerpoint 形状中找到文本并将其替换为 excel:

中单元格中的文本
Dim oSld As Slide
Dim oShp As Shape
Dim oTxtRng As TextRange
Dim oTmpRng As TextRange
Dim strWhatReplace As String, strReplaceText As String

 ' write find text
strWhatReplace = "xxxxx"
 ' write change text
strReplaceText = Sheet13.Range("C1").Value


        ' go during each slides
For Each oSld In ppPres.Slides
     ' go during each shapes and textRanges
    For Each oShp In oSld.Shapes
         ' replace in TextFrame
        Set oTxtRng = oShp.TextFrame.TextRange
        Set oTmpRng = oTxtRng.replace( _
        FindWhat:=strWhatReplace, _
        Replacewhat:=strReplaceText, _
        WholeWords:=True)

            Do While Not oTmpRng Is Nothing 
            Set oTxtRng = oTxtRng.Characters _
            (oTmpRng.Start + oTmpRng.Length, oTxtRng.Length)
            Set oTmpRng = oTxtRng.replace( _
            FindWhat:=strWhatReplace, _
            Replacewhat:=strReplaceText, _
            WholeWords:=True)
        Loop
    Next oShp
Next oSld

我不断收到运行时错误 13,类型不匹配 - 调试亮点 'For Each oShp In oSld.Shapes' 不确定我哪里错了

也就是说VBA认为oShp的变量类型和oSld.Shapes返回的类型不一样。代码对我来说看起来没问题,但您可以在立即 window 中键入此代码以仔细检查代码何时停止在该点:

?TypeName(oShp)
?TypeName(oSld.Shapes)

您应该会看到其中一个不是 Shape 类型(虽然我在阅读您的代码时看不出是怎么回事)。

ppPres 在哪里声明和设置?是 Presentation 类型的吗?

如果您引用了 Microsoft Excel Microsoft PowerPoint 对象库,则有两种类型的 Shape

似乎您已经确定了尺寸 Excel.Shape 但随后您得到 PowerPoint.Shape.

尝试Dim oShp As PowerPoint.Shape