Consulting

Results 1 to 3 of 3

Thread: Search for first instance and replace or insert

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

    Search for first instance and replace or insert

    Hi all, I am trying to search for the first instance of "Material" in my powerpoint slide titles, and add a carriage return after it. I have been trying to use the find and replace function, but it seems to be random about working. . Right now it replaces all the text in the slide titles with "Material" rather than inserting a carriage return. Any ideas?

    Sub Addlinetotitle()
    Dim sld As Slide
    Set sld = ActivePresentation.Slides(1)
    Dim shp As Shape
    Dim strFindWhat As String: strFindWhat = "Material"
    Dim strReplaceWhat As String: strReplaceWhat = "Material" & vbCr
    Dim treReplaced As TextRange
            For Each sld In ActivePresentation.Slides
                For Each shp In sld.Shapes
                    If shp.HasTextFrame Then
                        If shp.TextFrame.HasText Then
                            shp.TextFrame.TextRange.Text = ActivePresentation.Slides(1).Shapes(1).TextFrame.TextRange.Replace(FindWhat:=strFindWhat, ReplaceWhat:=strReplaceWhat)
                        End If
                    End If
                 Next
            Next sld
    End Sub
    Last edited by Aussiebear; 02-15-2022 at 04:32 PM. Reason: Add code tags to supplied code

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Does this work?

    Sub Addlinetotitle()
    Dim sld As Slide
    Set sld = ActivePresentation.Slides(1)
    Dim shp As Shape
    Dim lPos As Long
    Dim strTitle As String
    Dim strFindWhat As String: strFindWhat = "Material"
    Dim strReplaceWhat As String: strReplaceWhat = "Material" & vbCrLf
    If sld.Shapes.HasTitle Then
        strTitle = UCase(sld.Shapes.Title.TextFrame.TextRange.Text)
        lPos = InStr(strTitle, "MATERIAL")
        If lPos > 0 Then
            sld.Shapes.Title.TextFrame.TextRange.Characters(lPos + 8).InsertAfter vbCrLf
        End If
    End If
    End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    VBAX Regular
    Joined
    Feb 2022
    Posts
    6
    Location
    John Wilson
    Thank you so much! This does the trick! I did modify it to use "+7", but overall this works great!

Tags for this Thread

Posting Permissions

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