Select whole word at cursor position

  • Thread starter ל×ון גולדנברג
  • Start date
×

ל×ון גולדנברג

Hi,

Assume my cursor is placed within a random word of a document.

I'm looking for VBA commands which will select this whole word.
(Similar to double-clicking).

The code should select the word in 3 various cursor positions:

1) Within the word (between its characters).
2) At the beginning of the word (between the first character and the leading
space).
3) At the end of the word between the last character and the following space).

I'm familiar with the code for selecting the whole line, at the cursors
position:

Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend

but could not figure out how to implement it for a single word.

As more as I think - it sounds very similar to the inner Dictionary
implementation - saying: the recognition of the whole word from only placing
the cursor between its characters.

Farther more, if the code could return the words number - I would be able to
implement what ever I want by selecting: ActiveDocument.Words(x).select

Thanks, Elm
 
G

Graham Mayor

Dim oRng As Range
Dim i As Long
Selection.Words(1).Select
Set oRng = Selection.Range
With oRng
If oRng.Characters.Last = Chr(32) Then
oRng.End = oRng.End - 1
oRng.Select
End If
End With

will select the word at thge cursor and assign it to the range oRng

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

Jonathan West

???? ???????? said:
Hi,

Assume my cursor is placed within a random word of a document.

I'm looking for VBA commands which will select this whole word.
(Similar to double-clicking).

The code should select the word in 3 various cursor positions:

1) Within the word (between its characters).
2) At the beginning of the word (between the first character and the
leading
space).
3) At the end of the word between the last character and the following
space).

I'm familiar with the code for selecting the whole line, at the cursors
position:

Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend

but could not figure out how to implement it for a single word.

As more as I think - it sounds very similar to the inner Dictionary
implementation - saying: the recognition of the whole word from only
placing
the cursor between its characters.

Farther more, if the code could return the words number - I would be able
to
implement what ever I want by selecting: ActiveDocument.Words(x).select

Thanks, Elm

Can be done with a single line of code

Selection.Expand wdWord

To get the index number of the selected word, take a look at this article

Determine the index number of the current paragraph, table, section ...
http://www.word.mvps.org/FAQs/MacrosVBA/GetIndexNoOfPara.htm
 

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