Search code examples
arraysvbapowerpointuserform

How to take variables entered in the userform text box to the array text in the module using VBA in PowerPoint?


I have a macro to select slides, with required text, to move to a new presentation.

I have to extract 70-80 slides from a 500+ slides presentation. But I need to enter VB/Module to change the keywords/search text in the array. Is there a way I can move the text entered in the userform to the array (text)?

Userform to enter the keywords.

enter image description here

How do I link the text entered with the array list in the code?

Sub selct()

Dim pres1 As PowerPoint.Presentation, pres2 As PowerPoint.Presentation, 
pp  As Object
Set pp = GetObject(, "PowerPoint.Application")

Set pres1 = pp.ActivePresentation
Set pres2 = pp.Presentations.Add

Dim i As Long, n As Long
Dim TargetList

'~~>  Array of terms to search for
TargetList = Array("Agenda", "Review", "third", "etc")

'~~> Loop through each slide
For Each sld In pres1.Slides
    '~~> Loop through each shape
    For Each shp In sld.Shapes
        '~~> Check if it has text
        If shp.HasTextFrame Then
            Set txtRng = shp.TextFrame.TextRange

            For i = 0 To UBound(TargetList)
                '~~> Find the text
                Set rngFound = txtRng.Find(TargetList(i))

                '~~~> If found
                Do While Not rngFound Is Nothing
                    '~~> Set the marker so that the next find starts from here
                    n = rngFound.Start + 1
                    '~~> Chnage attributes
                    With rngFound.Font
                        .Bold = msoFalse
                        sld.Copy
                        pres2.Slides.Paste
                        '~~> Find Next instance
                        Set rngFound = txtRng.Find(TargetList(i), n)
                    End With
                Loop
            Next
        End If
    Next
Next
End Sub

Solution

  • The form objects are accessible even when the form is not shown, like this: Suppose you have a form with name UF1 with a textbox named TBforKeyWord, then you can access the textbox value at UF1.TBforKeyWord, so you might

     Redim Preserve TargetList(Ubound(TargetList) + 1)
     TargetList(Ubound(TargetList) = UF1.TBforKeyWord
    

    The logic is the same if you let the user enter multiple keywords but then you need to work a bit more on splitting (and parsing) the keywords.

    EDIT

     Dim text_array() As String 
     text_array = Split(SearchBox.Value, " ") 
    
     Dim iDimOld As Long
     Dim iDimNew As Long
     Dim i As Long
    
     iDimOld = Ubound(TargetList)
     iDimNew = iDimOld + Ubound(text_array) + 1
     Redim Preserve TargetList(iDimNew)
    
    
     ' Loop through each keyword in array 
     For i = 0 To Ubound(text_array)
          TargetList(iDimOld + i + 1) = text_array(i)
     Next