![](/img/trans.png)
[英]Find text in PowerPoint and Replace with text from a cell in Excel
[英]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,鍵入不匹配-調試突出顯示“對於在oSld.Shapes中的每個oShp”不確定我要去哪里哪里
這意味着VBA認為oShp的變量類型與oSld.Shapes返回的類型不同。 該代碼對我來說看起來不錯,但是您可以在立即窗口中鍵入此代碼,以再次檢查代碼在何時停止:
?TypeName(oShp)
?TypeName(oSld.Shapes)
您應該看到一個不是Shape類型的(盡管在閱讀代碼時我看不到)。
ppPres在哪里聲明和設置? 是Presentation類型的嗎?
如果您引用了Microsoft Excel 和 Microsoft PowerPoint對象庫,則有兩種類型的Shape
。
似乎您已確定Excel.Shape
尺寸,但隨后又得到PowerPoint.Shape
。
嘗試使用Dim oShp As PowerPoint.Shape
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.