I am trying to program code that basically works similarly to the Ctrl+F-search menu in Powerpoint. In this presentation, I have multiple profiles of different projects. Activating the macro should open a menu that allows you to input a search term which is scanned for on every slide in the presentation. The same term could be on multiple slides, for example if it is the name of the project leader.
I have written code which scans every slide and shape and then should select the text using the TextRange.find command. The problem is that after the first Exit Do command, the macro doesn't recognise any other End If, Next or Exit Do command. My code is as follows:
Sub Searchfunction()
Dim TagObject As String
Dim TexRng As TextRange
Dim TexFrm As TextFrame
Dim sld As Object
Dim shp As Object
Dim foundText As Object
Dim Check As Boolean, Counter As Long
Check = True
TagObject= InputBox("What are you searching for?")
Do
For Each sld In Application.ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.HasTextFrame Then
Set TexRng = shp.TextFrame.TextRange
Set foundText = TexRng.Find(TagObject)
Do While Not (foundText Is Nothing)
With foundText.Select
End With
Check = MsgBox("Continue search?", vbYesNo)
If Check = False Then GoTo SearchEnd
Exit Do
End If
Next
Next
Exit Do
MsgBox ("End of search, no further results.")
SearchEnd:
End Sub
Commenting out any End If still hands out the error message "End If without If-block" or "Next without for" despite there being an If, two Fors and a Do. How do I get VBA to recognise these blocks again?
The code now works as intended and selects the slide which the search object is on. My final code looks as follows:
Option Explicit
Sub Searchfunction()
Dim TagObject As String
Dim TexRng As TextRange
Dim TexFrm As TextFrame
Dim sld As Object
Dim shp As Object
Dim foundText As Object
Dim Check As Boolean
Dim Counter As Long
Counter = 1
Check = True
TagObject= InputBox("What are you searching for?")
For Each sld In Application.ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.HasTextFrame Then
Set TexRng = shp.TextFrame.TextRange
Set foundText = TexRng.Find(TagObject)
While Not (foundText Is Nothing)
ActivePresentation.Slides(Counter).Select
Check = MsgBox("Continue Search?", vbYesNo)
If Check = False Then GoTo SearchEnd
Set foundText = Nothing
Wend
End If
Next
Counter = Counter + 1
Next
MsgBox ("End of search, no further results.")
SearchEnd:
End Sub
If you want to search something while doing a presentation you need to replace the line ActivePresentation.Slides(Counter).Select
with ActivePresentation.SlideShowWindow.View.GotoSlide(Counter)
.