Extend selection till you reach a certain word

User avatar
stuck
Panoramic Lounger
Posts: 8240
Joined: 25 Jan 2010, 09:09
Location: retirement

Extend selection till you reach a certain word

Post by stuck »

How do I tweak this code:

Code: Select all

Selection.HomeKey Unit:=wdLine
Selection.Extend
Selection.MoveRight Unit:=wdWord
Selection.MoveLeft Unit:=wdCharacter, Count:=1
so that instead of it selecting just the first word of a line it selects all the words that occur before the word 'in', which will exist just once in the line but not always at the same point?

Thanks,

Ken

User avatar
StuartR
Administrator
Posts: 12662
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: extned selection till you reach a certain word

Post by StuartR »

This will do what you asked for, but if you run it on a line without "in" then it will go into an infinite loop.

Code: Select all

  Selection.HomeKey Unit:=wdLine
    Selection.Extend
    Do Until LCase(Trim(Selection.Words(Selection.Words.Count))) = "in"
        Selection.MoveRight Unit:=wdWord
    Loop
    Selection.MoveLeft Unit:=wdWord
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
StuartR


User avatar
stuck
Panoramic Lounger
Posts: 8240
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: extned selection till you reach a certain word

Post by stuck »

Thanks Stuart, meanwhile after some trial and error I came up with:

Code: Select all

Selection.HomeKey Unit:=wdLine
Selection.Extend
Selection.Find.Execute FindText:=" in ", Forward:=True, Wrap:=wdFindStop
Selection.MoveLeft Unit:=wdCharacter, Count:=4
which will also fails if it never finds " in " but, at the moment at least, the sentence in question does contain " in ". I'll cross the bridge of how to handle things when it doesn't have that key phrase in due course...

Ken

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: extned selection till you reach a certain word

Post by Rudi »

You could make use of the .Found property...

Code: Select all

Selection.HomeKey Unit:=wdLine
Selection.Find.Execute FindText:=" in ", Forward:=True, Wrap:=wdFindStop
If Selection.Find.Found Then
    Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
End If
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
stuck
Panoramic Lounger
Posts: 8240
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: extned selection till you reach a certain word

Post by stuck »

Ta! Presumably I could tweak FindText:= " in " to use a variable rather than fixed text?

If so that would allow me to use " in " as the default but if " in " wasn't found I could present an input box and the user could enter the separator text specific to their situation.

Ken

User avatar
HansV
Administrator
Posts: 78863
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: extned selection till you reach a certain word

Post by HansV »

Yes, FindText can be set to a variable or to an expression.
Best wishes,
Hans