簡體   English   中英

VBA 代碼可更改 Powerpoint 的每張幻燈片中的特定單詞

[英]VBA Code to change a particular word in every slides of a Powerpoint

我有一個代碼,可以很好地在一張特定幻燈片中將特定單詞更改為另一個單詞。 我想知道是否有解決方案,以便該詞被另一個詞替換,但在每張幻燈片中,而不是 1。 到目前為止,這是我的代碼:

Sub pres2()

    Dim PowerPointApp As Object
    Set PowerPointApp = CreateObject("PowerPoint.Application")

    Dim myPres As Object
    Set myPres = PowerPointApp.Presentations.Open("C:\Users\NAME\Desktop\PRESVBA\Présentation2.pptx")

    Dim sld As PowerPoint.Slide 
    Set sld = myPres.Slides(3) '<- this is the only slide where change occurs
    Dim shp As PowerPoint.Shape 

    For Each shp In sld.Shapes
        If shp.HasTextFrame Then
            If shp.TextFrame.HasText Then
                shp.TextFrame.TextRange.Text = Replace(shp.TextFrame.TextRange.Text, "Montant", "Amount")
            End If
        End If
    Next shp
End Sub

這很有效,但只將幻燈片 3 中的“Montant”一詞更改為“Amount”,而不是像我試圖做的那樣在每張幻燈片中更改

更新代碼

Sub pres2()

    Dim PowerPointApp As Object
    Set PowerPointApp = CreateObject("PowerPoint.Application")

    Dim myPres As Object
    Set myPres = PowerPointApp.Presentations.Open("C:\Users\NAME\Desktop\PRESVBA\Présentation2.pptx")

    Dim sld As PowerPoint.Slide
    Dim shp As PowerPoint.Shape

    For Each sld In myPres.Slides
        For Each shp In sld.Shapes
            If shp.HasTextFrame Then
                shp.TextFrame.TextRange.Text = Replace(shp.TextFrame.TextRange.Text, "Montant", "Amount")
            End If
        Next
    Next
End Sub

如果解決請標記為答案

您可以使用以下方法循環瀏覽所有幻燈片:

Dim sld As Slide   
For Each sld In ActivePresentation.Slides
     <individual slide code goes here>    
Next sld

您可以用指定的演示對象替換ActivePresentation

可以在此處找到有關slides對象的更多信息: https : //docs.microsoft.com/en-us/office/vba/api/powerpoint.slides

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM