Search code examples
vbapowerpoint

Powerpoint - find a specified word in a text box within a group and add a comment


I am looking to search for a specified word or phrase in a presentation and then add a comment to all slides where it appears. I have the below code which works well, however I want to be be able to search for text boxes that are in groups (the below code only searches in text boxes)

Any suggestions would be really appreciated.

Sub FindWordAndAddComment()
    Dim sld As Slide
    Dim shp As Shape
    Dim txtRng As TextRange, rngFound As TextRange
    Dim i As Long, n As Long
    Dim TargetList

'enter or word phrase here
    TargetList = Array("this is a test")

    For Each sld In Application.ActivePresentation.Slides
        For Each shp In sld.Shapes
            If shp.HasTextFrame Then
                Set txtRng = shp.TextFrame.TextRange

                For i = 0 To UBound(TargetList)
                    Set rngFound = txtRng.Find(TargetList(i))

                    Do While Not rngFound Is Nothing
                        n = rngFound.Start + 1
                                           
                        With sld.Comments.Add(12, 12, "found", "me", "'this is a test' has been found")
                        
                            
                            
                            Set rngFound = txtRng.Find(TargetList(i), n)
                        
                        End With
                    
               
                    
                    Loop
                Next
            End If
        Next
    Next
End Sub

Solution

  • This assumes that all groups have the default "Group" name:

    Dim sld As Slide
    Dim shp As Shape
    Dim txtRng As TextRange, rngFound As TextRange
    Dim i As Long, n As Long
    Dim TargetList(2) As String
    
    Sub FindWordAndAddComment()
    'enter or word phrase here
        TargetList(0) = "This is a test"
        TargetList(1) = "This is a text"
        TargetList(2) = "Here we go"
        For Each sld In Application.ActivePresentation.Slides
            For Each shp In sld.Shapes
                If InStr(shp.Name, "Group") <> 0 Then
                    For X = 1 To shp.GroupItems.Count
                        If shp.GroupItems(X).HasTextFrame Then
                            Set txtRng = shp.GroupItems(X).TextFrame.TextRange
                            FindTextAddComment
                        End If
                    Next X
                Else
                    If shp.HasTextFrame Then
                        Set txtRng = shp.TextFrame.TextRange
                        FindTextAddComment
                    End If
                End If
            Next
        Next
    End Sub
    
    Sub FindTextAddComment()
        For i = 0 To UBound(TargetList)
            Set rngFound = txtRng.Find(TargetList(i))
            Do While Not rngFound Is Nothing
                n = rngFound.Start + 1
                With sld.Comments.Add(12, 12, "found", "me", "'this is a test' has been found")
                    Set rngFound = txtRng.Find(TargetList(i), n)
                End With
            Loop
        Next
    End Sub