以下代码演示了如何通过 PowerPoint VBA 替换第一个幻灯片中的指定单元格(这里固定第二行第一列)中的所有 “Overall”:
Sub 替换Overall()
Dim tb As Table
Dim sld As Slide
Dim shp As Shape
Dim txtRange As TextRange
Const FIND_WHAT As String = "Overall"
Set sld = ActivePresentation.Slides(1)
Set shp = sld.Shapes(1)
' /* 第一个幻灯片的第一个形状中含有表格. */
If shp.HasTable Then
Set tb = shp.Table
Set shp = tb.Cell(2, 1).Shape
Set txtRange = shp.TextFrame.TextRange
Set txtRange = txtRange.Replace(FIND_WHAT, _
vbNullString, _
WholeWords:=msoTrue)
' /* 循环替换所有的 Overall 为空. */
Do While Not txtRange Is Nothing
Set txtRange = shp.TextFrame.TextRange
Set txtRange = txtRange.Replace(FIND_WHAT, _
vbNullString, _
WholeWords:=msoTrue)
Loop
End If
Set tb = Nothing
Set sld = Nothing
Set txtRange = Nothing
End Sub
运行效果:
上面只是提供了最最核心的演示代码,并不能用作你的实际需求,如果要替换多个演示稿文件中的多个幻灯片,那么就需要套两层循环去做,希望你可以通过这个来举一反三!
这个实在难倒我了