在PowerPoint演示文稿中编辑文本



我需要编写一个可以浏览演示文稿并将文本字符串的所有实例更改为其他的程序。因此,例如,在出现文本字符串"旧公司名称"的情况下,它替换为"新公司名称"。

我得到了如何自动化PowerPoint的一般想法,挑战在于很难沿着形状对象,我看不到存储此数据的明显属性(例如,例如,"文本"属性。)

有人可以将我指向正确的方向吗?

另外,是否有一种工具可以使其更容易地挖掘到办公产品的对象模型,也就是说,将特定文档的实例对象树走动?通常,我会使用Visual Studio调试器来执行此操作,但是由于它是COM顶部的薄层,因此您不能像在其他情况下那样轻松地将Object实例树在手表窗口中行走。有一个很好的工具可以帮助您吗?

ppt 2010如果很重要。

PowerPoint是自动化(使用VBA)的最棘手的办公应用程序之一,仅仅是因为您无法像Word and Excel一样录制宏。我发现学习对象模型的最佳方法是Web搜索和对象浏览器与VBide的组合(只需按F2)。

至于文本更换,一旦您知道,这是一种简单的情况。您可以循环遍历特定幻灯片中的所有形状,然后检查该形状的文本。(请注意,此代码实际上来自Excel工作簿,因此它具有Powerpoint参考文献,这是从PowerPoint中的必需的:

编辑: Steve对原始编辑仅搜索文本框提出了一个很好的观点,具体取决于您必须单独分组的每种类型对象的演示文稿,并在每种类型。仅仅是后部的疼痛并不是特别困难。

还要注意,根据演示的大小,可能需要一段时间才能循环浏览所有形状。我还将.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

最新更新