Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2005

Tip: Looking for answers? Try searching our database.

Selecting the current word (including underscore characters)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
davidS - 18 Nov 2005 15:09 GMT
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
Tony Jollans - 18 Nov 2005 16:24 GMT
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'.

--
Enjoy,
Tony

> 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
[quoted text clipped - 4 lines]
> Thanks,
> David
davidS - 18 Nov 2005 18:02 GMT
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
Tony Jollans - 18 Nov 2005 19:29 GMT
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.

--
Enjoy,
Tony

> Thanks Tony.
>
[quoted text clipped - 27 lines]
> ScreenTip:="",  _    TextToDisplay:="wdName"
> End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.