简体   繁体   English

希望在Powerpoint中查找文本并使用VBA在excel中使用单元格中的文本进行替换,但始终会出错

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

this part of my code should find text in powerpoint shapes and replace it with text from a cell in excel: 我的代码的这一部分应该找到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

I keep getting runtime error 13, type mismatch - the debug highlights 'For Each oShp In oSld.Shapes' Not sure where I am going wrong 我不断收到运行时错误13,键入不匹配-调试突出显示“对于在oSld.Shapes中的每个oShp”不确定我要去哪里哪里

That means VBA thinks that the variable type of oShp is not the same as the type returned by oSld.Shapes. 这意味着VBA认为oShp的变量类型与oSld.Shapes返回的类型不同。 The code looks ok to me but you could type this in the Immediate window to double check when the code stops at that point: 该代码对我来说看起来不错,但是您可以在立即窗口中键入此代码,以再次检查代码在何时停止:

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

You should see that one is not of type Shape (although I can't see how when reading your code). 您应该看到一个不是Shape类型的(尽管在阅读代码时我看不到)。

Where is ppPres getting declared and set? ppPres在哪里声明和设置? Is that of type Presentation? 是Presentation类型的吗?

If you have references to Microsoft Excel and Microsoft PowerPoint Object Library, then there are two types of Shape s. 如果您引用了Microsoft Excel Microsoft PowerPoint对象库,则有两种类型的Shape

Seems you have dimensioned Excel.Shape but then you get PowerPoint.Shape . 似乎您已确定Excel.Shape尺寸,但随后又得到PowerPoint.Shape

Try Dim oShp As PowerPoint.Shape . 尝试使用Dim oShp As PowerPoint.Shape

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM