批量查找和替换Power Point幻灯片的VBA代码时出错

时间:2017-05-30 03:30:28

标签: vba powerpoint powerpoint-vba

我试图在batch find and replace中为Power Point幻灯片编写VBA代码,但我收到以下错误:Compile Error Method or data member not found
< BR />

调试器突出显示PP.Shapes13中的形状。

我对VBA没有多少经验。我收集了以下想法:
* Getting Started with VBA in PowerPoint 2010(Office开发中心)
* Power Point VBA-Find & Replace(YouTube)
*&#34;简单宏从文件导入幻灯片&#34; @(VBA Express论坛)

Sub BatchFindReplace()
Dim shp As Shape
Dim strFileName As String
Dim strFolderName As String
Dim PP As Presentation
'Directory
strFolderName = "C:\Users\Emma\Desktop\temp1"
strFileName = Dir(strFolderName & "\*.ppt*")
Do While Len(strFileName) > 0
Set PP = Presentations.Open(strFolderName & "\" & strFileName)

'Find and Replace Code
For Each shp In PP.Shapes
If shp.HasTextFrame Then
    If shp.TextFrame.HasText Then
        shp.TextFrame.TextRange.Text = Replace(shp.TextFrame.TextRange.Text, "W", "kkk")
    End If
End If
Next

PP.Close
strFileName = Dir
Loop
End Sub

1 个答案:

答案 0 :(得分:1)

属性.Shapes不是Presentation的成员,而是Slide

的成员
'~~> Open the relevant powerpoint file
Set PP = Presentations.Open(strFolderName & "\" & strFileName)

'~~> Change this to the relevant slide which has the shape
Set PPSlide = PP.Slides(1)

For Each shp In PPSlide.Shapes
    Debug.Print shp.Name
Next shp

如果您想使用所有幻灯片中的所有形状,则必须循环播放幻灯片。

Dim sld As Slide

'~~> Open the relevant powerpoint file
Set PP = Presentations.Open(strFolderName & "\" & strFileName)

For Each sld In PP.Slides
    For Each shp In sld.Shapes
        Debug.Print shp.Name
    Next shp
Next