PDA

View Full Version : Macro to find a string and copy sentence containing string



joetacos25
01-02-2020, 12:04 PM
Hi,

I need help creating a Powerpoint macro.
I would like to search in a Powerpoint document for a string. If this string is found I want to copy the sentence containing the string in a new slide in the same powerpoint document.

I am using Microsoft PowerPoint for Office 365 MSO.

John Wilson
01-02-2020, 02:33 PM
This would be the basis but you might need to adapt it yourself.


Sub chex()
Dim osld As Slide
Dim newsld As Slide
Dim oshp As Shape
Dim otr As TextRange
Dim b_found As Boolean
Dim s As Long
Const Findstring As String = "This is me" ' change to suit
For Each osld In ActivePresentation.Slides
For Each oshp In osld.Shapes
If oshp.HasTextFrame Then
If oshp.TextFrame.HasText Then
Set otr = oshp.TextFrame.TextRange
For s = 1 To otr.Sentences.Count
If InStr(LCase(otr.Sentences(s)), LCase(Findstring)) > 0 Then
b_found = True
End If
If b_found Then Exit For
Next s
End If
End If
If b_found Then Exit For
Next oshp
If b_found Then Exit For
Next osld
If b_found Then
Set newsld = ActivePresentation.Slides.Add(1, ppLayoutText)
newsld.Shapes(1).TextFrame.TextRange = "Sentence Found"
newsld.Shapes(2).TextFrame.TextRange = otr.Sentences(s)
End If
End Sub

joetacos25
01-03-2020, 08:05 AM
Thanks John for your solution!.

I tested and worked pretty well. Your solution helped me a lot.

Thanks for your time and support. :)

Drake888
02-05-2020, 07:54 AM
Hello, I faced the same problem, could not understand what the problem was. Your decision really helped, thanks, have a nice day!

MarceloTagut
02-13-2020, 10:54 PM
How to disable the VBA command "SAVE AS" in PowerPoint 2020, can someone help me? Please

John Wilson
02-14-2020, 01:35 PM
That has nothing to do with the question and there is no PPT 2020.

Paul_Hossler
02-16-2020, 03:09 PM
How to disable the VBA command "SAVE AS" in PowerPoint 2020, can someone help me? Please

We're glad to try and answer questions, but it's not polite to hijack another thread instead of starting your own new one, especially when your question is so different

Top left of the first page in each forum is a button (+Post New Thread) that you can use

Also, take a few minutes and read the FAQ link in my signature