Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

selection.words(x).select on multiple words? 1

Status
Not open for further replies.

565u

Technical User
Apr 25, 2005
46
CZ
Hi and thanks for reading my post!

I use
selection.words.count
to find out how many words are in a selection. Then I would like to select them (in case only part of some word is selected initially), but the only way I can figure out is by selecting the first word and then extending the selection. Isn't there some nice syntax to select several words using
selection.words(x).select
please?

Many thanks in advance for any kind help!

Best regards,

Pavel
 
Hi Pavel,

If selection.words.count returns a value, then the value represents the number of words already selected. You don't have to figure out how to selected them, since that's a;ready been done!

Perhaps you could explain what you're trying to do, since it's generally good practice to avoid using selections.

Cheers
Paul Edstein
[MS MVP - Word]
 
Many thanks for your reply.
I am not very good at VBA so I may be wrong, but lets use a example:
"This is an example sentence."
let's say I make this selection (capitals show selection)
"This is an exaMPLE SENTEnce."
Now .count returns 2 because 2 words are in selection. But if I want to manipulate those 2 words I can't. I will be manipulating the selected characters.
That is why I want to select the words in selection. Aslong as it is just one word, I can use the selection.words(x).select, but what if I would like to select more words (like in the example above). I am trying to figure out if there is a syntax that would allow that. Something like:
selection.words(1-2).select
selection.words(1, 2).select
etc.

Many thanks in advance!

Pavel
 
Hi Pavel,

I wouldn't choose to manipulate a range this way, but you could use something based on:
Code:
Sub Demo()
With Selection
  .Start = .Words.First.Start
  .End = .Words.Last.End
  MsgBox .Text
End With
End Sub
this actually extends the selection. My own preference would be to use something closer to:
Code:
Sub Demo()
Dim myRng As Range
Set myRng = Selection.Range
With myRng
  .Start = .Words.First.Start
  .End = .Words.Last.End
  MsgBox .Text
End With
End Sub
This leaves the original selection intact, but allows you to manipulate the word range based on that selection.

Cheers
Paul Edstein
[MS MVP - Word]
 
WOW! Wow! and WOW again.
That's such a nice solution! Thank you so much for sharing this!!!!

Best wishes and a very happy new year!

:)
Pavel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top