Subscribe to andrewsw's Blog        RSS Feed

My Word, Auto-Complete

Icon 3 Comments
I started to write some notes in MS Word. When writing notes on technical subjects there are quite a few words that are repeated often, sometimes long or challenging words. I got side-tracked and decided I wanted an "auto-complete" feature, so that I could type just a few letters and press, for example, Ctrl-Space and have the word completed for me.

Word has AutoText and AutoCorrect and it is sensible to use built-in features as much as possible. However, I wasn't interested in taking the time to define these entries. I just wanted my code to look through the document to find the matching word; as long as I type enough characters to uniquely identify the word, this should be fine. Rather than looking through the document every time, I would add the current abbreviation to a dictionary.

The Dictionary collection object is in the Microsoft Scripting Runtime. Be careful to qualify it with Scripting.Dictionary otherwise it will assume the default Word Dictionary. The Dictionary will disappear on exiting Word, but that's fine; it's also useful in case the entries start to go awry.

I assigned the shortcut Ctrl-Space to my macro, which was assigned to ResetChar. Ctrl-Shift-Right is an alternative, assigned to WordRightExtend. To achieve this, record a macro pressing the Keyboard button that appears to assign the keyboard shortcut, then just stop, and edit, the macro. I stored the macro (and related function) in the Normal template, so that it is available to any document.

So, just type a few letters of a word and press Ctrl-Space to complete it, assuming that you have already typed the word you need earlier in the document.

Public dict As Scripting.Dictionary

Sub AutoCompletion()
    'References, Microsoft Scripting Runtime
    Dim sWord As String
    Dim sNewWord As String
    If dict Is Nothing Then
        Set dict = New Scripting.Dictionary
    End If
    Application.ScreenUpdating = False
    Selection.MoveLeft wdWord, 1, wdExtend
    sWord = RTrim(Selection.Words(1).Text)

    If dict.Exists(sWord) Then
        sNewWord = dict.Item(sWord)
        sNewWord = GetWord(sWord)
        If sNewWord <> "" Then
            dict.Add sWord, sNewWord
        End If
    End If
    If sNewWord <> "" Then
        If Selection.End + 1 = Activedocument.Range.End Then
            sNewWord = sNewWord & " "
        End If
        Selection.Text = sNewWord
    End If
    Selection.MoveRight wdWord, 1
    Application.ScreenUpdating = True
End Sub

Function GetWord(sFindWord As String)
    Dim sWord As String
    Dim wd As Range
    For Each wd In Activedocument.Words
        If Len(RTrim(wd.Text)) > Len(sFindWord) Then
            If Left(wd.Text, Len(sFindWord)) = sFindWord Then
                GetWord = RTrim(wd.Text)
                Exit Function
            End If
        End If
    Next wd
    GetWord = ""
End Function

The code only attempts to follow the inserted word with a space if the cursor is at the end of the document. (Otherwise, earlier in the document, a space or other separator would already be present.)

This could be extended I suppose. The dictionary entries could be added to AutoText, so that a yellow prompt-box appears. I even considered, for a moment, the possibilty of providing a drop-down list of words to choose from. Maybe in a floating TextBox, or a control, or even in the statusbar. Personally, though, the current functionality is fine for my purpose. I should get back to writing my notes!

3 Comments On This Entry

Page 1 of 1


17 December 2015 - 12:35 PM
Does that smart filter out do-nothing works like 'the' or 'a'?


17 December 2015 - 01:00 PM
The user supplies the smarts, only seeking specific, uncommon, words.


17 December 2015 - 01:04 PM
It would be easy enough to modify to demand a minimum three-letter key, but I might one day write some notes about xylophones ;)
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

November 2020

222324252627 28


    Recent Entries

    Recent Comments

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)