Word selection | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 24, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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 1997If 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 1move to beginning of word
    End ifno need to check stuff twice, move to starting point and go check right side
    if iCount <> 0 Thenin case we start at beginning of word
        ActiveDocument.Selection.CharRight dsMove, iCount
    End ifcheck 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
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.