简单地说,因为无法用Word和Excel记录宏,所以Powerpoint是自动化(使用VBA)的更复杂的Office应用程序之一。我发现学习对象模型的最好方式是将网页搜索和对象浏览器与VBIDE结合使用(只需按F2键)。
至于文字替换,一旦你知道,这是一个容易的情况。您可以遍历特定幻灯片中的所有形状,然后检查该形状的文本。 (注意,此代码实际上来自一个Excel工作簿,因此它具有Powerpoint
引用这没有必要从PowerPoint中:
编辑:史蒂夫使得有关原始编辑一个非常好的问题只能搜索文本框,根据在您的演示文稿设置中,您必须单独对每种类型的对象进行排序,并在每种类型上实施自定义替换,而不是特别难于仅在后方产生痛苦
另请注意,根据演示文稿的大小,它可能需要一段时间来循环所有的形状,我也使用了.HasTextFrame
/.HasTable
和.Type
的组合,所以你可以看到两种类型。
Sub ReplaceTextShape(sFindText As String, sNewText As String, ppOnSlide As PowerPoint.Slide)
Dim ppCurShape As PowerPoint.Shape
For Each ppCurShape In ppOnSlide.Shapes
If ppCurShape.HasTextFrame Then
ppCurShape.TextFrame.TextRange.Text = VBA.Replace(ppCurShape.TextFrame.TextRange.Text, sFindText, sNewText)
ElseIf ppCurShape.HasTable Then
Call FindTextinPPTables(ppCurShape.Table, sFindText, sNewText)
ElseIf ppCurShape.Type = msoGroup Then
Call FindTextinPPShapeGroup(ppCurShape, sFindText, sNewText)
''Note you'll have to implement this function, it is an example only
ElseIf ppCurShape.Type = msoSmartArt Then
Call FindTextinPPSmartArt(ppCurShape, sFindText, sNewText)
''Note you'll have to implement this function, it is an example only
ElseIf ppCurShape.Type = msoCallout Then
'etc
ElseIf ppCurShape.Type = msoComment Then
'etc etc
End If
Next ppCurShape
Set ppCurShape = Nothing
End Sub
然后替换整个演示文稿中的所有文本:
Sub ReplaceAllText(ppPres As PowerPoint.Presentation)
Dim ppSlide As PowerPoint.Slide
For Each ppSlide In ppPres.Slides
Call ReplaceTextShape("Hello", "Goodbye", ppSlide)
Next ppSlide
Set ppSlide = Nothing
End Sub
和示例代码表中的替换文本:
Sub FindTextinPPTables(ppTable As PowerPoint.Table, sFindText As String, sReplaceText As String)
Dim iRows As Integer, iCols As Integer
With ppTable
iRows = .Rows.Count
iCols = .Columns.Count
For ii = 1 To iRows
For jj = 1 To iCols
.Cell(ii, jj).Shape.TextFrame.TextRange.Text = VBA.Replace(.Cell(ii, jj).Shape.TextFrame.TextRange.Text, sFindText, sReplaceText)
Next jj
Next ii
End With
End Sub
你能使用VBA,而不是C#作为好?如果是这样,请添加VBA标签,以便获得VBA答案。 –
好主意。我做到了。 –
您是否只有旧的PPT文件或新的PPTX文件? –