Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    May 2002
    Location
    London, Gtr London, England
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I select 1 word? (2000 9.0.4402/SR1)

    I must be missing something really obvious. All I want to do is to allow the user to click inside a word or number then run a macro to select that word or number. The trouble is that selecting a word also selects all trailing spaces and that numbers with decimal points are treated as 2 words separated by the decimal point ( I guess Word thinks it's a full stop). I can't have that as I am inserting a hyperlink on the word/number.

    To get round these problems I have written a routine that moves to the start of the word/number one symbol at a time ( as I can't find a "goto start of word" method) and then selects to the end of the word/number one symbol at a time and this does work unless the word/number is in the first table cell on a row in which case I can't detect when I am at the start of the word as I use what is to the left of the first letter of the word/number to determine that I am at the front of the word/number and there is no "start of cell marker"

    I think I have missed some obvious method and I am now getting caught up in compensating for an increasingly bizarre and convoluted routine.

    Any idea's?

    Any help appreciated.

    Thanks Richard

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: How do I select 1 word? (2000 9.0.4402/SR1)

    You can use

    Selection.MoveLeft Unit:=wdWord

    to move to the start of a word.

  3. #3
    2 Star Lounger
    Join Date
    May 2002
    Location
    Atlanta, Georgia, USA
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How do I select 1 word? (2000 9.0.4402/SR1)

    How about something like this:

    Selection.Words(1).Select
    If InStr(Selection.Text, " ") Then Selection.MoveLeft unit:=wdCharacter, Extend:=wdExtend
    If Val(Selection.Text) <> 0 Then
    ' insert code to check for decimal points
    End If

    This selects the "word" regardless of location, then checks for a trailing space. If found, it excludes the trailing space.

    The next If statement checks if the "word" is a numeric value. I'm not sure what you want to do in this case, perhaps check to see if the following character is a decimal point.

    Anyway, hope this gives you a simpler structure to work with. One of the hardest parts of teaching yourself VBA is learning all of the objects and how they can be used. I still have trouble keeping them straight at times.

    Good luck!
    Bob
    Writing Consultant / Word Template Designer
    Words Connect

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: How do I select 1 word? (2000 9.0.4402/SR1)

    <P ID="edit" class=small>Edited by jscher2000 on 11-Jun-02 18:47.</P>The following will work when you are sure the surrounding characters are spaces:

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

    The first parameter of these methods can contain any number of "stop" characters, but I'm not sure how you could specify a table cell boundary. Suppose you substitute this for the first line:

    Selection.MoveStart wdWord, -1

    Cool. And you can use the corresponding method to extend forward:

    Selection.MoveEnd wdWord, 1

    But it will include the next space. So you then can "trim back" using a loop:

    Do While Right(Selection.Text, 1) = " "
    Selection.MoveEnd wdCharacter, -1
    Loop

    And about those decimals. Very difficult if the decimal is to the left of the insertion point and there is no preceding space. Your character checking routine might be needed. <font color=red>Actually, Klaus gives a possible solution in <!post=Post #148242,148242>Post #148242<!/post>.</font color=red> But to check the right side, you could add this loop before the space removing loop:

    Do While Right(Selection.Text, 1) = "."
    Selection.MoveEnd wdWord, 1
    Loop

    In the end, it's not better than your method, unless the words are incredibly long. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •