在Powerpoint中使用InputBox替换

问题描述

我想使用用户输入替换所有幻灯片中的“仪器”一词。 当我将Replacement =“ ppp”设置为下面的代码时,但是当我将其更改为Replacement = InputBox时,该乐器不会被用户输入替换。

有什么建议吗?

Sub Replaceinstrument()
    Dim sld As Slide
    Dim shp As Shape
    Dim Replacement As String
    

    For Each sld In ActivePresentation.Slides
    For Each shp In sld.Shapes

    
    Replacement = InputBox("to change")
    
    If Replacement = "" Then Exit Sub
    
               
    If shp.HasTextFrame Then
        If shp.TextFrame.HasText Then
             With shp.TextFrame.TextRange
              .Replace "instrument",Replacement
                    
             End With
                             
            End If
        End If
        
    Next shp
    
Next

End Sub

解决方法

因为循环中有InputBox,所以它在演示文稿中的每个形状上都在运行。这是按工作顺序排列的:

Sub Replaceinstrument()
    Dim sld As Slide
    Dim shp As Shape
    Dim Replacement As String

    Replacement = InputBox("to change")
    If Replacement = "" Then Exit Sub
    
    For Each sld In ActivePresentation.Slides
        For Each shp In sld.Shapes
            If shp.HasTextFrame Then
                If shp.TextFrame.HasText Then
                    shp.TextFrame.TextRange.Replace "instrument",Replacement
                End If
            End If
        Next shp
    Next
End Sub