It might be simpler to have the search button pop up a form in the first place with a
text box control for the "Find:" text and a "Find Next" button the user can click to
initiate the search and perhaps a "New Search" button they can use to "clear" the
search.
I've written a little function and some test code that'll do the bulk of the work.
You call the function once with just the search string as a parameter to find the first
ocurrence of the text; if found, it returns the index of the slide the text was found
on; zero otherwise.
For subsequent searches, you call the function with the same search text and two zeros
as parameters.
If for some reason you want to start searching at a slide or shape number other than 1
you can pass the starting numbers as parameters as well.
Note that since it only checks each shape once, it's only going to find a given search
string once per shape, even if the shape has more than one instance of the string.
The test routine's written to run in normal view. For slide show view, youd' want to
use:
SlideShowWindows(1).View.GoToSlide lTemp
instead
Option Explicit
Sub TestTheFunction()
Dim lTemp As Long
' find the first occurrence
lTemp = FindTheText("now")
If lTemp > 0 Then
ActiveWindow.View.GotoSlide lTemp
MsgBox "Click to continue"
End If
' find the next occurrence(s)
While lTemp > 0
lTemp = FindTheText("now", 0, 0)
If lTemp > 0 Then
ActiveWindow.View.GotoSlide lTemp
MsgBox "Click to continue"
End If
Wend
End Sub
Function FindTheText(sFindString As String, _
Optional lStartSlide As Long = 1, _
Optional lStartShape As Long = 1) As Long
' Returns the slide index of the slide the searchtext was found on
' if any
Dim oSl As Slide
Dim oSh As Shape
Dim x As Long
Dim y As Long
' Static vars to record highest previously searched
' slide and shape numbers
Static lSlideNum As Long
Static lShapeNum As Long
' are we starting a new search/resetting?
' if so, lStartSlide/lStartshape will both be 1
Select Case lStartSlide + lStartShape
Case 0
' we're continuing the search
' don't reset the counters
Case 2
' new search; reset both counters
lSlideNum = 1
lShapeNum = 1
Case Else
' we're starting from a specific position
lSlideNum = lStartSlide
lShapeNum = lStartShape
End Select
With ActivePresentation
For x = lSlideNum To .Slides.Count
Set oSl = .Slides(x)
With oSl
For y = lShapeNum To .Shapes.Count
Set oSh = .Shapes(y)
With oSh
If .HasTextFrame Then
If .TextFrame.HasText Then
If InStr(UCase(.TextFrame.TextRange.Text), _
UCase(sFindString)) > 0 Then
' FOUND ONE; record position
lSlideNum = x
lShapeNum = y + 1
' return slide index
FindTheText = oSl.SlideIndex
' and stop
Exit Function
End If ' Instr
End If ' Has text
End If ' has text frame
End With ' oSh
Next ' Shape
End With ' slide
lShapeNum = 1
Next ' slide
End With
' text not found, return 0
FindTheText = 0
End Function
==============================
PPT Frequently Asked Questions
http://www.pptfaq.com/
PPTools add-ins for PowerPoint
http://www.pptools.com/
.