
Custom Toolbar Button which Highlights Next & Previous Sentence in Document...
[posted and emailed]
Hi Thomas,
You don't need to search for the full stops. Word has a 'Sentences'
collection, so the coding is pretty straightforward:
Sub SelectPrevSentence
Dim r As Range
Set r = Selection.Sentences(1)
r.Move Unit:=wdSentence, Count:=-2
r.Sentences(1).Select
End Sub
Sub SelectNextSentence
Dim r As Range
Set r = Selection.Sentences(1)
r.Move Unit:=wdSentence, Count:=1
r.Sentences(1).Select
End Sub
Note that a legitimate period in the middle of a sentence (say, in
an abbreviation) is incorrectly interpreted as the end of a sentence,
no matter what follows it. While you could try to code around this,
it'd probably be more trouble than it's worth.
Hope this helps.
Word MVP FAQ: http://www.mvps.org/word
Please reply ONLY to the newsgroup. MVPs do not work for Microsoft.
Userform demystification: http://www.speakeasy.org/~mtangard/userforms.html
"Life is nothing if you're not obsessed." --John Waters
Quote:
> Hello,
> I have created a custom toolbar which contains various buttons. One of
> these buttons is supposed to highlight the previously written sentence in
> the document. I presume this feature would need to somehow detect the last
> full-stop to successfully highlight the correct section of the document.
> As an add-on to the "Previous Sentence" feature, I would also, like to add a
> "Next Sentence" feature aswell.
> Could anyone provide any insight into how I might code this type of feature,
> in terms of how make the button "detect" the next and/or previous sentence.
> I know how to use the highlight feature in Word so that is not a problem,
> its just the "sentence" part that is confusing me slightly...
> I would appreciate anyone's advice on this issue...
> Many thanks,
> Thomas Evans