Selecting the current word (including underscore characters)

D

davidS

I am trying to write a Word macro that selects the current word and
then does some processing on that word. What method can I use to
select the current word (ie the one where the cursor is). The words
may contain undercores so I want it to select the whole word. For
example the word may be T_demog.

Thanks,
David
 
T

Tony Jollans

Hi David,

If you want other than Word's definition of a 'word', you must define your
own. You can extend the selection with something like

Selection.MoveEndUntil "., " & vbCr, wdForward
Selection.MoveStartUntil " " & vbCr, wdBackward

This will look forward for a full stop, comma, space, or carriage return and
backward for a space or carriage return. You will need to also look for
other characters depending on your text - semicolon, question mark, etc.-
and what you want to include in your 'word'.
 
D

davidS

Thanks Tony.

I have a follow up question. Perhaps you can answer it.

I am trying to set a hyperlink on the word selected.
I would like the address of the hyperlink to be: "tables\the word
selected.sas"

For example
suppose the cursor was on the word "t_demog". I'd like to set the
address to
tables\t_demog.sas and the TextToDisplay to be t_demog.

I am trying to set a variable wdName to the text selected and then use
that
to set the address and the TextToDiplay.

See my code below.

Sub set_hyperlink()
'
' set_hyperlink Macro
' Macro recorded 11/18/2005 by David
'
Dim wdName As Characters
Selection.MoveEndUntil "., " & vbCr, wdForward
Set wdName = Selection.Copy
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
Address:="tables\" & "wdName" & ".sas" , SubAddress:="",
ScreenTip:="", _ TextToDisplay:="wdName"
End Sub
 
T

Tony Jollans

Hi David,

If you have the' word' selected then Selection.Text is a string value which
you can use directly or assign to a string variable if you wish.
 

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