How to retract selection or range from trailing space and punctuation

L

Larry

Whenever I want to select just a word and retract the selection from any
punctuation or spaces after the word, I need something like this:

Selection.MoveEndWhile cset:=",.!?""", Count:=wdBackward

Is there any all-purpose statement that does this directly, without the
user having to specify the punctuation? What I'm talking about would be
sort of the equivalent of the Trim function, only a Trim function that
would work directly on a selection in a document. It would be neat if
there was such a thing.

Thanks,
Larry
 
J

Jonathan West

Larry said:
Whenever I want to select just a word and retract the selection from any
punctuation or spaces after the word, I need something like this:

Selection.MoveEndWhile cset:=",.!?""", Count:=wdBackward

Is there any all-purpose statement that does this directly, without the
user having to specify the punctuation? What I'm talking about would be
sort of the equivalent of the Trim function, only a Trim function that
would work directly on a selection in a document. It would be neat if
there was such a thing.

Thanks,
Larry

Hi Larry,

Something like this should do the trick

Sub ShrinkSelection()
Do While Selection.Characters.Last.Text Like "[!a-zA-Z]"
If Selection.Type <> wdSelectionNormal Then Exit Do
Selection.MoveEnd Unit:=wdCharacter, Count:=-1
Loop
End Sub


The Selection.Type check ensures that the macro doesn't keep going even
after the selectyion has shrunk to an insertion point.
 
L

Larry

Hi Jonathan,

Thanks. So, it comes down to a choice between these two. Do you see
any advantage of one of the other?


System.Cursor = wdCursorIBeam
Do While Selection.Characters.Last.Text Like "[!a-zA-Z0-9]"
If Selection.Type <> wdSelectionNormal Then Exit Do
Selection.MoveEnd Unit:=wdCharacter, Count:=-1
Loop

or

System.Cursor = wdCursorIBeam
Selection.MoveEndWhile cset:=" ,.;:!?-""" & Chr(30) & Chr(151) &
Chr(148), _
Count:=wdBackward





Jonathan West said:
Larry said:
Whenever I want to select just a word and retract the selection from any
punctuation or spaces after the word, I need something like this:

Selection.MoveEndWhile cset:=",.!?""", Count:=wdBackward

Is there any all-purpose statement that does this directly, without the
user having to specify the punctuation? What I'm talking about would be
sort of the equivalent of the Trim function, only a Trim function that
would work directly on a selection in a document. It would be neat if
there was such a thing.

Thanks,
Larry

Hi Larry,

Something like this should do the trick

Sub ShrinkSelection()
Do While Selection.Characters.Last.Text Like "[!a-zA-Z]"
If Selection.Type <> wdSelectionNormal Then Exit Do
Selection.MoveEnd Unit:=wdCharacter, Count:=-1
Loop
End Sub


The Selection.Type check ensures that the macro doesn't keep going even
after the selectyion has shrunk to an insertion point.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
 
J

Jonathan West

Larry said:
Hi Jonathan,

Thanks. So, it comes down to a choice between these two. Do you see
any advantage of one of the other?


System.Cursor = wdCursorIBeam
Do While Selection.Characters.Last.Text Like "[!a-zA-Z0-9]"
If Selection.Type <> wdSelectionNormal Then Exit Do
Selection.MoveEnd Unit:=wdCharacter, Count:=-1
Loop

or

System.Cursor = wdCursorIBeam
Selection.MoveEndWhile cset:=" ,.;:!?-""" & Chr(30) & Chr(151) &
Chr(148), _
Count:=wdBackward


There's not all that much in it. If you include Option Compare Text at the
top of your module, the first of the two routines will properly recognise
accented characters as part of a word, and will exclude everything else, so
you don't need to think about remembering to include all possible
punctuation characters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top