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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Obtain range of word contaning insertion point

Status
Not open for further replies.

Nelviticus

Programmer
Sep 9, 2003
1,819
GB
Is there a way to obtain the Range of the word that contains the insertion point even if it's at the end of the word?

This is what I have at the moment:

Code:
    Dim objRange As Range
    
    Set objRange = Selection.Range
    
    objRange.Expand wdWord

It works if the insertion point is at the beginning or in the middle of the word, but if it's at the end (i.e. you've just typed it) it doesn't pick it up.

Regards

Nelviticus
 
Your code works if the insertion point is at the end of a WORD, just not if it's at the end of a paragraph. You can test for that using

if objrange.Characters(1)=vbCr then ...

for example, you might just move the range one character to the left in that case:

if objrange.Characters(1)=vbCr then selection.MoveLeft wdCharacter,1
objrange.expand wdWord

Of course, there are situations where this might not work. See how it does for you.


Rob
[flowerface]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top