Consulting

Results 1 to 5 of 5

Thread: Find and Replace for all instances of "Find" list

  1. #1
    VBAX Regular
    Joined
    Feb 2022
    Posts
    6
    Location

    Find and Replace for all instances of "Find" list

    I am trying to run a find/replace using arrays, and it works, but only for the first instances on each slide. I have a slide title with duplicate words, but only the first is replaced, so I'm hoping for some help to get both words replaced.
    Thanks!

        'LOOP THROUGH EACH SLIDE
        For Each sld In objPPT.ActivePresentation.Slides
            For Y = LBound(FindArray1) To UBound(FindArray1)
                fnd = FindArray1(Y)
                rplc = ReplaceArray1(Y)
                For Each shp In sld.Shapes
                    If shp.HasTextFrame Then
                        If shp.TextFrame.HasText Then
                            Set TxtRng = shp.TextFrame.TextRange.Find(fnd, 0, True, WholeWords:=msoTrue)
                            If Not TxtRng Is Nothing Then
                                Do
                                    Set tmprng = TxtRng.Replace(FindWhat:=fnd, ReplaceWhat:=rplc, WholeWords:=True, MatchCase:=True)
                                Loop While Not tmprng Is Nothing
                            End If
                        End If
                    End If
                Next shp
            Next Y
        Next sld

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,095
    Location
    Replace DOES stop at the first instance unless you tell it to continue

                               'first instance
                                    Set tmpRng = txtRng.Replace(FindWhat:=fnd, ReplaceWhat:=rplc, WholeWords:=True, MatchCase:=True)
                                    Do
                                    'others
                                    Set tmpRng = txtRng.Replace(FindWhat:=fnd, ReplaceWhat:=rplc, After:=tmpRng.Start + tmpRng.Length, WholeWords:=True, MatchCase:=True)
                                    Loop While Not tmpRng Is Nothing
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    Quote Originally Posted by John Wilson View Post
    Replace DOES stop at the first instance unless you tell it to continue

                               'first instance
                                    Set tmpRngSoul Knight Mod APK = txtRng.Replace(FindWhat:=fnd, ReplaceWhat:=rplc, WholeWords:=True, MatchCase:=True)
                                    Do
                                    'others
                                    Set tmpRng = txtRng.Replace(FindWhat:=fnd, ReplaceWhat:=rplc, After:=tmpRng.Start + tmpRng.Length, WholeWords:=True, MatchCase:=True)
                                    Loop While Not tmpRng Is Nothing
    Thank you so much, you helped me too.

  4. #4
    Quote Originally Posted by John Wilson View Post
    Replace DOES stop at the first instance unless you tell it to continue

                               'first instance
                                    Set tmpRng = txtRng.Replace(FindWhat:=fnd, ReplaceWhat:=rplc, WholeWords:=True, MatchCase:=True)
                                    Do
                                    'others
                                    Set tmpRng = txtRng.Replace(FindWhat:=fnd, ReplaceWhat:=rplc, After:=tmpRng.Start + tmpRng.Length, WholeWords:=True, MatchCase:=True)
                                    Loop While Not tmpRng Is Nothing

    Very helpful, indeed.

  5. #5
    Banned VBAX Newbie
    Joined
    Feb 2024
    Posts
    2
    Location
    Quote Originally Posted by Raybob View Post
    I am trying to run a find/replace using arrays, and it works, but only for the first instances on each slide. I have a slide title with duplicate words, but only the first is replaced, so I'm hoping for some help to get both words replaced.
    Thanks!

        'LOOP THROUGH EACH SLIDE
        For Each sld In objPPT.ActivePresentation.Slides
            For Y = LBound(FindArray1) To UBound(FindArray1)
                fnd = FindArray1(Y)
                rplc = ReplaceArray1(Y)
                For Each shp In sld.Shapes
                    If shp.HasTextFrame Then
                        If shp.TextFrame.HasText Then
                            Set TxtRng = shp.TextFrame.TextRange.Find(fnd, 0, True, WholeWords:=msoTrue)
                            If Not TxtRng Is Nothing Then
                                Do
                                    Set tmprng = TxtRng.Replace(FindWhat:=fnd, ReplaceWhat:=rplc, WholeWords:=True, MatchCase:=True)
                                Loop While Not tmprng Is Nothing
                            End If
                        End If
                    End If
                Next shp
            Next Y
        Next sld

    Thanks for sharing it with us!
    Last edited by Aussiebear; 03-22-2024 at 11:58 PM. Reason: Removed spam

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •