Word selection

This is a simple macro that emulates a mouse double-click. It selects the word under the caret - very useful for keyboard jockeys like Brian.


Sub WordSelect() 
        'DESCRIPTION: Selects a word like a double click with mouse
        'Very handy for keyboard users. Not the prettiest
        'or quickest (I heard BASIC was slow!  :] )
        'macro but it works better than the built in word right after Alt-M or
        'C in BRIEF, etc and more accurate, also helps RSI sufferers like me.
        '
        'Valid characters are all upper case and lower case letters
        'and the underscore "_" character, to change this behav add toLegalChars.
        '
        'Rev 1 - Got it working without checking for individual chars
        '   instead used the numerical comparisons
        '
        'Rev 2 - Added numbers to be valid during string search
        '   Now supports all valid C/C++ chars duh!
        '
        'Rev 3 - (7/12/98) Added check for first column, also tidy'd
        '    up code for checking for valid letters from ranges
        '    to using InStr w/ valid chars (Thanks David Cotton)
        '    BTW I never claimed to be a VB guy!   :)
        '
        'This insignificant macro was written by B.Sturk on Aug 31 1997
        'If you find an easier way to accomplish what this macro
        'does or a quicker way, or a bug, please let me know!

        'email: bsturk@nh.ultranet.com
        'http://www.nh.ultranet.com/~bsturk

    iCount =0

    LegalChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"

    ExecuteCommand "SelectChar"    'start char selection (not in loop)

    ActiveDocument.Selection.CharLeft dsExtend
    cSelect = ActiveDocument.Selection

    do while InStr(LegalChars, cSelect) <> 0

        iCurrentCol =  ActiveDocument.Selection.CurrentColumn

        If iCurrentCol = 1 Then
            iCount = iCount + 1
            Exit Do
        End if

        iCount = iCount + 1

        ExecuteCommand "SelectChar"
        ExecuteCommand "SelectChar"

        ActiveDocument.Selection.CharLeft dsExtend    'move to next char

        cSelect = ActiveDocument.Selection
    Loop

    ' Now that we're done moving left and checking we need to go to the beginning
    ' of the word to move right and check for legal chars. If we're in column 1 we're at the
    ' beginning

    If iCurrentCol <> 1 Then
        ActiveDocument.Selection.CharRight 1    'move to beginning of word
    End if

    'no need to check stuff twice, move to starting point and go check right side
    if iCount <> 0 Then   ' in case we start at beginning of word
        ActiveDocument.Selection.CharRight dsMove, iCount
    End if

    'check to the right of the cursor placement
    ExecuteCommand "SelectChar"
    ExecuteCommand "SelectChar"

    ActiveDocument.Selection.CharRight dsExtend
    cSelect = ActiveDocument.Selection

    do while InStr(LegalChars, cSelect) <> 0

        iCount = iCount + 1

        ExecuteCommand "SelectChar"
        ExecuteCommand "SelectChar"

        ActiveDocument.Selection.CharRight dsExtend
        cSelect = ActiveDocument.Selection
    Loop

    'stop char select mode, move left one, and select entire word
    ExecuteCommand "SelectChar"

    ActiveDocument.Selection.CharLeft dsMove, 1       ' We've overstepped by one move back

    ExecuteCommand "SelectChar"

    ActiveDocument.Selection.CharLeft dsExtend, iCount

End Sub


Comments

  • Here is my attempt at word selection

    Posted by Legacy on 08/29/2001 12:00am

    Originally posted by: Ender Wiggin

    This is my attempt at writing a word selection macro. it takes the opposite approach from the other two listed here. It finds the Delimeters on either end of the current word, and selects everything between them. this approach just seemed more natural to me, and I think I like the results. However, I'm not a VB Programmer, and I'm pretty sure that this is not the most efficient. Oh well, it works. feel 
    
    free to change it and tell everyone what is wrong.


    Sub WORDSelect()

    'DESCRIPTION: Emulates a mouse double click over a word at the cursor.
    ActiveDocument.Selection.Cancel

    ' Valid C++ identifiers
    Delimiters = " `~@$%^&*()-=+[{]}\|;:',<.>/?" + vbLf + vbTab + vbNewLine + Chr(34)

    WordStartNotFound = 2
    WordEndNotFound = 2

    ' find/goto the start of a word to the left DON'T select anything
    ' the start of a word is the character right of the first delimiter found
    while WordStartNotFound > 1

    ActiveDocument.Selection.CharLeft dsExtend
    cSelect = ActiveDocument.Selection

    ' Look left for Delimiter character if one is found then stop slecting left.
    if InStr( Delimiters, cSelect ) <> 0 Then

    ' If a Delimiter was found the reverse the last slection
    ActiveDocument.Selection.CharRight

    WordStartNotFound = 0

    End If

    ActiveDocument.Selection.Cancel

    Wend

    Count = 0

    ' find/goto the end of a word to the left DO select everything
    ' the end of a word is the character left of the first delimiter found
    while WordEndNotFound > 1

    ActiveDocument.Selection.CharRight dsExtend
    cSelect = ActiveDocument.Selection

    ' Look left for Delimiter character if one is found then stop slecting left.
    if InStr( Delimiters, cSelect ) <> 0 Then

    ' If a Delimiter was found the reverse the last slection
    ActiveDocument.Selection.CharLeft dsExtend

    WordEndNotFound = 0

    End If

    ActiveDocument.Selection.Cancel
    Count = Count + 1
    Wend

    if ( Count > 1 ) Then
    ActiveDocument.Selection.CharLeft dsExtend, Count - 1
    End If
    End Sub

    Reply
  • shift+ctrl works too..

    Posted by Legacy on 08/03/2000 12:00am

    Originally posted by: Anders Montonen

    Hold down SHIFT for selection, CTRL for word hopping and press RIGHT arrow. Of course, if the caret is in the middle of a word, you might have to press CTRL+LEFT first.
    Of course, using macros is chest-hair-growing, but a bit unnecessary in this case..

    Reply
  • Great stuff!!! Thanks.

    Posted by Legacy on 10/07/1999 12:00am

    Originally posted by: Luis Garcia

    Great stuff!!! Thanks.

    Reply
  • Great! I was missing this for years. Here a fix for VC6

    Posted by Legacy on 04/07/1999 12:00am

    Originally posted by: wech

    In DevStudio6 the macro did no longer work (after running the macro you would be in a mode where every cursor
    move would extend the selection further which is normally not desired and hard to get rid of).
    
    

    Search for the following line:
    ActiveDocument.Selection.CharLeft dsMove, 1 ' We've overstepped by one move back

    and replace the code in the lines with the following:

    'WeCh This down not work any more:
    'ExecuteCommand "SelectChar"
    'ActiveDocument.Selection.CharLeft dsExtend, iCount

    'WeCh: first go to the beginning of the word (as I prefer the cursor
    'to be after selection at the end of the word
    ActiveDocument.Selection.CharLeft dsMove, iCount

    'now select on char after the other to the right until the whole word
    'is selected
    for i=1 to iCount
    ExecuteCommand "CharRightExtend"
    next
    End Sub

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds