PDA

View Full Version : [SOLVED:] Search for first instance and replace or insert



Raybob
02-15-2022, 02:59 PM
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

John Wilson
02-16-2022, 06:11 AM
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

Raybob
02-16-2022, 12:18 PM
John Wilson :bow:
Thank you so much! This does the trick! I did modify it to use "+7", but overall this works great!