473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
1 3947
Stewart Ross
2,545 Expert Mod 2GB
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

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

Similar topics

1
by: John Martin | last post by:
This should be an easy match, but I'm lost. I want to perform a kind of grouping of the data in a datagrid object, so that only the first occurrence of a value in column one is visible. The...
2
by: Alexey Smirnov | last post by:
Regex.Replace("BUILDING IN PARIS, IN LONDON" ,"\bIN\b","???"); returns "BUILDING ??? PARIS, ??? LONDON" I need to replace the first match only, to have "BUILDING ??? PARIS, IN LONDON" How can...
1
by: hairlessOrphan | last post by:
Hi! Is there any way to get the Replace() function in MSSQL to replace only the first occurence of a substring? For example, my Name column has the following data: Bob BobBob And I want to...
3
by: Peter Thornqvist | last post by:
How would I design a regular expression to find the first occurence of a specific pattern and not look for more matches? -- Regards, Peter
2
by: gauravguleria | last post by:
wht's the program which takes two strings using command line arguments and finds the occurence of second string in the first string.The program should return the starting position of first...
7
by: mike.aldrich | last post by:
Hi folks, I am trying to read the first occurence of non-whitespace in a file, within a zipfile. Here is my code: zipnames = glob.glob("<search_dir>*") for zipname in zipnames: z =...
1
by: Lollygag | last post by:
Hi folks. Please try not to laugh at my web design noobness, I'm a photoshop monkey, but I thought I'd have a go at updating this website to learn css & html, it's far more complex than I'd...
4
by: mercuryshipzz | last post by:
Hi, My objective is to get the line number of the first occurance of the search pattern. my test.txt contains: ..... .................. total rows.... ................... ..
2
by: ameesha | last post by:
hi all.. Can any one help me.. i am asked to develop a software in which one option is to search the first occurence of a particular word within a text file..and save the word and preceeding...
6
by: tshad | last post by:
I have a filename that I want to extract the 1st set of numbers up to either a ".", "-", "_" or "~" and make that an int. Or I guess easier just take all the values that are 0-9 up to the 1st non...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.