none
Select a word PROPERLY using a Range RRS feed

  • Question

  • How do I properly select the word where the cursor is? Currently I'm doing this:

    rng.MoveStart(Word.WdUnits.wdCharacter, 1)     ' Move start point one step right to make sure we are inside the word, even if the cursor started at left edge of word.
    rng.MoveStart(Word.WdUnits.wdWord, -1)         ' Move start point one word left - always takes us to start of word.
    rng.MoveEnd(Word.WdUnits.wdCharacter, -1)      ' Move end point one step left to make sure we are inside the word, even if the cursor started at right edge of word.
    rng.MoveEnd(Word.WdUnits.wdWord, 1)            ' Move end point one word right - always takes us to start of next word.
    rng.MoveEnd(Word.WdUnits.wdCharacter, -1)      ' Move end point one step left, takes us to the end of "our" word

    Note the last line that excludes the trailing whitespace character that is otherwise included in the word.

    This works in the normal case, but when the word is last on a line or last in a table cell, the range resulting from rng.MoveEnd(Word.WdUnits.wdWord, 1) does not include the trailing whitespace (since there is none after the word). And thus the rng.MoveEnd(Word.WdUnits.wdCharacter, -1) excludes the last character in the word instead.

    I'm suspecting this will be the same for other sitautions as well, such as fields, end-of-document, etc.

    1. I could check if the last character of the range is a word delimiter before excluding it, but I don't know all word delimiters, especially not in other languages than my own.
    2. I could also check the character to the right of the range for a chr(13), chr(11), etc, but again, I'm not sure in what situations the character following the word will not be included in the range.

    Is there a better method to select the word? I need it to be in a range btw, not a string, because I need to work with it in the document.

    Or, failing that, is there a list of all word delimiters?

    [Edit: VSTO, Office 2010, Windows 7, Visual Studio 2015.]

    Wednesday, December 16, 2015 1:28 PM

Answers

  • >>>How do I properly select the word where the cursor is?

    According to your description, you could use Selection.Start to get starting character position of a selection where the cursor is, then you could select word. you could refer to below code:

    Set myRange = ActiveDocument.Range(Start:=Selection.Start, End:=ActiveDocument.Range.End)
    Set aword = myRange.Words(1)
      
    If (aword.Text = Chr(13)) Then
       If (myRange.Words.Count >= 2) Then
          Set aword = myRange.Words(2)
       End If
    End If
    aword.Select
    '    For Each aWord In myRange.Words
    '     If aWord.Text = "World" Then aWord.Select
    '    Next aWord

    • Marked as answer by David_JunFeng Tuesday, December 29, 2015 2:49 PM
    Thursday, December 17, 2015 9:30 AM
  • Try something based on the following VBA code:

    With Selection
      .Words(1).Select
      While .Characters.Last Like "[ " & Chr(160) & "]"
        .MoveEnd wdCharacter, -1
      Wend
    End With


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by David_JunFeng Tuesday, December 29, 2015 2:49 PM
    Friday, December 18, 2015 1:39 AM

All replies

  • Normally, I'd do this:

        rng.Words(1).Select();

    How does that not work for you?


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Wednesday, December 16, 2015 4:07 PM
    Moderator
  • Normally, I'd do this:

        rng.Words(1).Select();

    How does that not work for you?


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    That does the same as my more covoluted solution:

    • If the range is in the middle or begining of a paragraph, it selects the wors and the trailing whitespace.
    • If the range is in the last word in a paragraph (or last word before a linebreak, or last word in tabel cell, etc.), the result does not include the paragraph marker etc.

    I'm sorry if I was unclear; I want just the word, without any trailing or leading whitespace or other delimiters.

    Also, I want to apologize for the ambigous use of the term "select". I do work with a range, and when I used the term "select", I was referring to the act of creating a range that includes the word, not actually selecting the word in the document itself. I could work with a selection as well, I guess, if necessary.

    Here's a summary of what I need to do:

    1. The starting point is a range that is a part of a word. It can be an insertion point (i.e. the start and end is the same) somewhere in the word, or it can be one or more contigous characters.
    2. That insertion point is inside a word, or immediately to the left or right of it. No characters outside of the word is part of the range.
    3. I want to select the whole word where the insertion point is.
    4. Only the word, never any leading or trailing whitespace.
    5. The issue is the difference between when the starting range is in a word amidst other text, and when it is at the end of a paragraph (or linebreak-separated lines, table cells, etc.) In the latter case, the trailing separator is not included. This makes it hard to separate out the trailing separator, since it is not always there.
    Thursday, December 17, 2015 8:31 AM
  • >>>How do I properly select the word where the cursor is?

    According to your description, you could use Selection.Start to get starting character position of a selection where the cursor is, then you could select word. you could refer to below code:

    Set myRange = ActiveDocument.Range(Start:=Selection.Start, End:=ActiveDocument.Range.End)
    Set aword = myRange.Words(1)
      
    If (aword.Text = Chr(13)) Then
       If (myRange.Words.Count >= 2) Then
          Set aword = myRange.Words(2)
       End If
    End If
    aword.Select
    '    For Each aWord In myRange.Words
    '     If aWord.Text = "World" Then aWord.Select
    '    Next aWord

    • Marked as answer by David_JunFeng Tuesday, December 29, 2015 2:49 PM
    Thursday, December 17, 2015 9:30 AM
  • Try something based on the following VBA code:

    With Selection
      .Words(1).Select
      While .Characters.Last Like "[ " & Chr(160) & "]"
        .MoveEnd wdCharacter, -1
      Wend
    End With


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by David_JunFeng Tuesday, December 29, 2015 2:49 PM
    Friday, December 18, 2015 1:39 AM