By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,503 Members | 2,788 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,503 IT Pros & Developers. It's quick & easy.

[PowerPoint VBA] Positioning the cursor to the first occurrence of the keyword

P: 6
Hi all,

Basically, I want to run a macro that will search for the keyword and move the cursor to the first occurence within the text body.

In Word, I was able to do this easily thanks to the Macro Recorder. However, this was not possible in Powerpoint since no recorder is available.

As I searched in the Help section, I found the code to find the keyword, as below:

Expand|Select|Wrap|Line Numbers
  1. Sub searchMacro()
  2. For Each sld In Application.ActivePresentation.Slides
  3.     For Each shp In sld.Shapes
  4.         If shp.HasTextFrame Then
  5.             Set txtRng = shp.TextFrame.TextRange
  6.             Set foundText = txtRng.Find(FindWhat:="CompanyX")
  7.             Do While Not (foundText Is Nothing)
  8.                 With foundText
  9.                     .Font.Bold = True
  10.                     Set foundText = _
  11.                         txtRng.Find(FindWhat:="CompanyX", _
  12.                         After:=.Start + .Length - 1)
  13.                 End With
  14.             Loop
  15.         End If
  16.     Next
  17. Next
  18. End Sub
  19.  
What this code does is to find the occurences of the keyword in the text, make them bold but the mouse cursor is still at the first page.

Please help me point out what I need to modify to move the cursor to the first occurence of the keyword being found.

Any input is greatly appreciated. Thank you so much in advance.

-D
Jan 6 '12 #1
Share this Question
Share on Google+
1 Reply


Expert Mod 2.5K+
P: 2,545
This answer is probably too late to assist the poster, but is provided to clear up the question.

The example code in the question comes straight from the Powerpoint VBA help file - but it is incorrect. Attempting to run it as written leads to an infinite loop. The FindFirst function does not return Nothing for the textrange object value if there is no match in the tests I have performed (on PowerPoint 2007). Instead it returns an empty string. In addition to this problem, on the last occurrence of the text concerned Find repeatedly cycles between the start of the last matching text range and the end, again creating an infinite loop.

The revised code below correctly terminates for any number of instances of the text "CompanyX" included on slides in a presentation.

Expand|Select|Wrap|Line Numbers
  1. Sub searchMacro()
  2. Dim sld As Slide
  3. Dim shp As Shape
  4. Dim txtRng As TextRange
  5. Dim foundtext As TextRange
  6. Dim lngStart As Long
  7.  For Each sld In Application.ActivePresentation.Slides
  8.      For Each shp In sld.Shapes
  9.          If shp.HasTextFrame Then
  10.              Set txtRng = shp.TextFrame.TextRange
  11.              lngStart = 0
  12.              Set foundtext = txtRng.Find(FindWhat:="CompanyX")
  13.              Do While Not (foundtext = "") And (foundtext.Start > lngStart)
  14.                  With foundtext
  15.                      .Font.Bold = True
  16.                      Set foundtext = _
  17.                          txtRng.Find(FindWhat:="CompanyX", _
  18.                          After:=.Start + .Length - 1)
  19.                      lngStart = .Start
  20.                  End With
  21.              Loop
  22.          End If
  23.      Next
  24.  Next
  25. End Sub
Although I have not yet found how to move the cursor itself to the first occurrence of the text concerned, the first occurrence of the search text can be selected using the following sub (again based on the 'CompanyX' exemplar in the helpfile).

Expand|Select|Wrap|Line Numbers
  1. Sub SelectFirst()
  2. Dim sld As Slide
  3. Dim shp As Shape
  4. Dim txtRng As TextRange
  5. Dim foundtext As TextRange
  6. Dim lngStart As Long
  7.  For Each sld In Application.ActivePresentation.Slides
  8.      For Each shp In sld.Shapes
  9.          If shp.HasTextFrame Then
  10.              Set txtRng = shp.TextFrame.TextRange
  11.              lngStart = 0
  12.              Set foundtext = txtRng.Find(FindWhat:="CompanyX")
  13.              If foundtext <> "" Then
  14.                 sld.Select
  15.                 shp.Select
  16.                 foundtext.Select
  17.                 Exit Sub
  18.              End If
  19.          End If
  20.      Next
  21.  Next
  22. End Sub
Note that for the Select method of the textrange object to work correctly the slide and shape objects concerned must be selected first, as shown. If you omit those steps a run-time error occurs due to failure to select the outer objects before trying to select the inner textrange object.

To generalise the search, replace the string literal "CompanyX" in the code above with a reference to an appropriately-set string variable containing the search term you are looking for.

-Stewart
Feb 16 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.