Reputation: 405
Currently using the PowerPoint VSTO API to search for words within a PowerPoint presentation.
Bumped into an issue when searching a TextRange
within a shape for a string with the expectation of more than one result to return.
The text I am doing the find on is (I have bolded the words to bring attention to them, they are not like this in the original):
This is some text
- Colour vs color
- Some filler text
- Some other filler text
- Formalize vs formalise
var shape = presentation.Slides[1].Shapes[2];
var findResults = shape.TextFrame.TextRange.Find("vs", 0, MsoTriState.msoFalse, MsoTriState.msoFalse);
var count = findResults.Count; //Expected 2, but only found 1
Any thoughts? I was expecting two results but at the moment I am only getting one, the first range "Colour vs color".
Edit: Solution Putting this here in case someone else comes across this (or I forget).
Did some more digging and I discovered the int after
argument in the Find()
VSTO API. Essentially the after
argument moves the range to search area on a number of characters you pass it. So passing in the end range of the first result and calling the .Find()
again returns the next instance I was looking for.
For example:
var shape = presentation.Slides[1].Shapes[2];
var findResults1 = shape.TextFrame.TextRange.Find("vs", 0, MsoTriState.msoFalse, MsoTriState.msoFalse);
//Returns the first vs instance "Colour vs color"
var findResults2 = shape.TextFrame.TextRange.Find("vs", (findResults.Start + findResults.Length), MsoTriState.msoFalse, MsoTriState.msoFalse);
//Returns the second vs instance "Formalize vs formalise"
Upvotes: 1
Views: 105