# Spell Checking

> Underline misspelled words in VB6 with the SpellCheckText event, check them with TX Spell .NET and correct them from a context menu or spelling dialog.

- **Product:** TX Text Control ActiveX
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control ActiveX > Document Editor > Spell Checking
- **4125 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/spell-checking/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/spell-checking/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/spell-checking/llms-full.txt

---

A red zigzag line under a word tells the user that it is misspelled. `TXTextControl` draws that line, counts the misspelled words and replaces them with corrections, but it does not decide which words are wrong: since version 16.0, it hands the changed text to your code, and your code asks a spell checker and reports the misspelled words back.

What follows explains this interface, fills it with a simple check of your own and then with TX Spell .NET, the spell checker of Text Control, through the TX Spell .NET ActiveX Package. The last sections correct misspelled words from a context menu and from a spelling dialog box of your own.

### How Spell Checking Works

TX Text Control ActiveX has no dictionary of its own. Its spell checker interface lets you connect any spell checker to a `TXTextControl`, and it works in three steps:

1. You switch spell checking on with the [EnableSpellChecking](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/enablespellchecking-property/llms-full.txt) property.
2. Each time the text of the document changes, the control raises the [SpellCheckText](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/spellchecktext-event/llms-full.txt) event. Its `Text` parameter holds the text to check.
3. Your event handler checks the text and returns the positions of the misspelled words in the `MisspelledWordPositions` parameter. `TXTextControl` underlines these words with a red zigzag line.

`MisspelledWordPositions` is an array of `Long` values that your handler creates. It holds two entries for each misspelled word, the start position and the end position, and both are one-based indexes into `Text`. For the text "This iss an exxample", with the misspelled words "iss" and "exxample", the array holds the four values 6, 8, 13 and 20.

After that, the control keeps track of the misspelled words. Your code reads their number with [MisspelledWords](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/misspelledwords-property/llms-full.txt), their position and length with [MisspelledWordStart](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/misspelledwordstart-property/llms-full.txt) and [MisspelledWordLength](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/misspelledwordlength-property/llms-full.txt), and corrects or ignores a word with [MisspelledWordDelete](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/misspelledworddelete-method/llms-full.txt). All of these members count the misspelled words from the beginning to the end of the document, starting with 1.

Since version 20.0, a piece of text can be formatted for a language, which spell checking and hyphenation use. The [Locale](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/locale-property/llms-full.txt) property sets it for the selected text as a Windows locale identifier (LCID), and the [LocaleDialog](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/localedialog-method/llms-full.txt) method lets the user choose it.

### Turn On Spell Checking

`EnableSpellChecking` is a `Boolean` property. Set it to **True** in the Properties window of Visual Basic, as the samples of the TX Spell .NET ActiveX Package do, or in code, for instance when the form loads:

```vbscript
Private Sub Form_Load()
    ' Raise the SpellCheckText event when the text changes
    TXTextControl1.EnableSpellChecking = True
End Sub
```

As long as `EnableSpellChecking` is **False**, the `SpellCheckText` event does not occur.

### Report Misspelled Words to the Control

The following handler shows what the control expects from the `SpellCheckText` event. It splits the text at spaces and asks the function `IsMisspelled` about each word. In a real application, `IsMisspelled` calls a spell checker; here it knows two misspelled words, so you can try the handler with the text "This iss an exxample":

```vbscript
Private Sub TXTextControl1_SpellCheckText(ByVal Text As String, MisspelledWordPositions As Variant)
    Dim Positions() As Long
    Dim Count As Long
    Dim WordStart As Long
    Dim I As Long

    WordStart = 1
    For I = 1 To Len(Text) + 1
        ' A space or the end of the text ends a word
        If I > Len(Text) Or Mid$(Text, I, 1) = " " Then
            If IsMisspelled(Mid$(Text, WordStart, I - WordStart)) Then
                ReDim Preserve Positions(Count + 1)
                Positions(Count) = WordStart     ' one-based start
                Positions(Count + 1) = I - 1     ' one-based end
                Count = Count + 2
            End If
            WordStart = I + 1
        End If
    Next I

    ' Report the misspelled words, if there are any
    If Count > 0 Then MisspelledWordPositions = Positions
End Sub

Private Function IsMisspelled(Word As String) As Boolean
    ' Stand-in for a call to a spell checker
    IsMisspelled = (Word = "iss" Or Word = "exxample")
End Function
```

The code works as follows:

- The handler declaration is the one the samples of the TX Spell .NET ActiveX Package use: `Text` is passed by value as a `String`, and `MisspelledWordPositions` is a `Variant` that the handler assigns the array to.
- `Positions` collects two `Long` values for each misspelled word. The start is the one-based position of the word's first character in `Text`, and the end is the position of its last character.
- For "This iss an exxample", the handler returns 6, 8, 13 and 20, and `TXTextControl` underlines "iss" and "exxample".

### Check With TX Spell .NET

TX Spell .NET is the spell checking component of Text Control for .NET applications, available as [TX Spell .NET for Windows Forms](https://docs.textcontrol.com/tx-spell-dotnet-winforms/llms-full.txt). To use it from Visual Basic 6, Text Control released the *TX Spell .NET ActiveX Package* in January 2012, as the blog article [TX Spell .NET ActiveX Package Released - Spell Checking in VB6](https://www.textcontrol.com/blog/2012/01/09/tx-spell-net-activex-package-released-spell-checking-in-vb6/) announced. It is a Visual Studio project that wraps TX Spell .NET for Windows Forms in a COM-visible control, `AxTXSpellChecker`, whose members Visual Basic 6 can call.

The package comes as source code, and you need TX Spell .NET for Windows Forms to build it. Since November 2012 the source has been open for changes by its users, as [TX Spell .NET ActiveX Package Goes CodePlex](https://www.textcontrol.com/blog/2012/11/21/tx-spell-net-activex-package-goes-codeplex/) explains; today it is the GitHub repository [TXSpell.ActiveX-Package](https://github.com/TextControl/TXSpell.ActiveX-Package). The repository holds the wrapper project *AxTXSpell* and two Visual Basic 6 samples, *Simple* and *DialogAndContextMenu*. Its *first\_steps.md* file describes how to build the wrapper in Visual Studio, register its type library with `regasm` and open the samples. The wrapper project references TX Spell .NET for Windows Forms 10.0 and targets .NET Framework 4.0.

With a reference to the wrapper's type library, which the samples list as **TX Spell .NET ActiveX Package** among their references, the whole spell checking code of the *Simple* sample is this:

```vbscript
Dim TXSpell1 As AxTXSpell.AxTXSpellChecker

Private Sub Form_Load()
    Set TXSpell1 = New AxTXSpellChecker
End Sub

Private Sub TXTextControl1_SpellCheckText(ByVal Text As String, MisspelledWordPositions As Variant)
    TXSpell1.Check (Text)
    MisspelledWordPositions = TXSpell1.MisspelledWordPositions
End Sub
```

The code works as follows:

- `Form_Load` creates one `AxTXSpellChecker` for the form. `EnableSpellChecking` is set to **True** in the Properties window of `TXTextControl1`.
- In the `SpellCheckText` event, the wrapper's `Check` method checks the text.
- The wrapper's `MisspelledWordPositions` property returns the start and end positions in the format that the event expects, so the handler assigns them directly.

The members of `AxTXSpellChecker` come from the source code of the package, not from TX Text Control ActiveX. Besides `Check`, the samples use `CreateSuggestions` and `GetSuggestions` for correction suggestions and `OptionsDialog` for the options dialog box of TX Spell .NET.

### Correct a Word From a Context Menu

The *DialogAndContextMenu* sample opens a context menu with suggestions when the user right-clicks a misspelled word. Its `MouseUp` handler moves the input position, the place where the caret stands, to the clicked point, selects the misspelled word there and fills three menu items with suggestions:

```vbscript
Private Sub TXTextControl1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    On Error Resume Next

    If Button = 2 Then
        TXTextControl1.SelStart = TXTextControl1.InputPosFromPoint(X, Y)
    End If

    If TXTextControl1.MisspelledWordAtInputPos = 0 Or Button <> 2 Then
        Exit Sub
    End If

    ' Select the misspelled word
    TXTextControl1.SelStart = TXTextControl1.MisspelledWordStart(TXTextControl1.MisspelledWordAtInputPos)
    TXTextControl1.SelLength = TXTextControl1.MisspelledWordLength(TXTextControl1.MisspelledWordAtInputPos)

    ' Get suggestions from TX Spell .NET
    TXSpell1.CreateSuggestions (TXTextControl1.SelText)
    suggestionsArray = TXSpell1.GetSuggestions

    mnuWord1.Caption = suggestionsArray(0)
    mnuWord2.Caption = suggestionsArray(1)
    mnuWord3.Caption = suggestionsArray(2)

    PopupMenu mnu_Spell
End Sub

Private Sub mnuWord1_Click()
    TXTextControl1.MisspelledWordDelete TXTextControl1.MisspelledWordAtInputPos, mnuWord1.Caption
End Sub
```

The sample also hides empty menu items and shows "No suggestions found." when there is no suggestion; the excerpt leaves this out. The code works as follows:

- A right click (`Button = 2`) moves the input position to the clicked point with [InputPosFromPoint](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/inputposfrompoint-method/llms-full.txt).
- [MisspelledWordAtInputPos](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/misspelledwordatinputpos-property/llms-full.txt) returns the number of the misspelled word at the input position. If there is none there, the property returns the next misspelled word, or the first one when none follows.
- To select a misspelled word, you assign `MisspelledWordStart` to [SelStart](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/selstart-property/llms-full.txt) and `MisspelledWordLength` to [SelLength](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/sellength-property/llms-full.txt). [SelText](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/seltext-property/llms-full.txt) then returns the word.
- When the user chooses a suggestion, `MisspelledWordDelete` replaces the word's text with the suggestion and removes the word from the list of misspelled words.

### Build a Spelling Dialog Box

The second form of the *DialogAndContextMenu* sample, a dialog box named **Spelling**, goes through the misspelled words of the document one by one. It always works on misspelled word number 1: once that word has been corrected or ignored, it leaves the list, and the next word becomes number 1. The excerpt below shows the **Change** and **Ignore Once** buttons. It is adapted from the sample: it leaves out the preview of the word in its line and the list of suggestions, and it ignores a word with an empty string, as the reference describes.

```vbscript
Private Sub NextMisspelledWord()
    If TXTextControl1.MisspelledWords = 0 Then
        MsgBox "Spell checking complete."
    Else
        ' Select the first remaining misspelled word
        TXTextControl1.SelStart = TXTextControl1.MisspelledWordStart(1)
        TXTextControl1.SelLength = TXTextControl1.MisspelledWordLength(1)
    End If
End Sub

Private Sub btnChange_Click()
    ' Replace the word with the selected suggestion
    TXTextControl1.MisspelledWordDelete 1, listSuggestions.Text
    NextMisspelledWord
End Sub

Private Sub btnIgnoreOnce_Click()
    ' Keep the word's text, remove it from the list
    TXTextControl1.MisspelledWordDelete 1, ""
    NextMisspelledWord
End Sub
```

The code works as follows:

- `MisspelledWords` returns the number of misspelled words in the document. When it is 0, the check is complete.
- `MisspelledWordDelete` takes the number of the word and a corrected text. With a corrected text, it replaces the word's text. With an empty string, the word's text stays as it is, and the word is only removed from the list of misspelled words.
- The method returns **True** when it could remove the word.

In the sample, the dialog box also sets [HideSelection](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/hideselection-property/llms-full.txt) of the document's `TXTextControl` to **False** when it loads, so that the selected word stays visible while the dialog box has the input focus. The list of suggestions comes from the `CreateSuggestions` and `GetSuggestions` members of the wrapper.

### Spell Checkers of Earlier Versions

Before the spell checker interface of version 16.0, TX Text Control ActiveX left spell checking to components of other vendors, which a special API of the control let them build for it, as the blog article [Spell Checking in TX Text Control ActiveX](https://www.textcontrol.com/blog/2004/08/06/spell-checking-in-tx-text-control-activex/) of 2004 describes. The obsolete [VTSpellCheck](https://docs.textcontrol.com/tx-text-control-activex/reference/obsolete-members/vtspellcheck-method/llms-full.txt) method and [VTSpellDictionary](https://docs.textcontrol.com/tx-text-control-activex/reference/obsolete-members/vtspelldictionary-property/llms-full.txt) property started the VT Speller of VisualTools, which was not part of the Text Control package and is no longer available. Use the spell checker interface described above instead.

### Key Members

| Member | Description |
|---|---|
| [EnableSpellChecking](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/enablespellchecking-property/llms-full.txt) | Switches spell checking on or off. When it is **True**, the `SpellCheckText` event occurs. |
| [SpellCheckText](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/spellchecktext-event/llms-full.txt) | Occurs when text has changed and must be checked; the handler returns the positions of the misspelled words. |
| [MisspelledWords](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/misspelledwords-property/llms-full.txt) | Returns the number of misspelled words in the document. |
| [MisspelledWordStart](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/misspelledwordstart-property/llms-full.txt) | Returns the zero-based position of a misspelled word. |
| [MisspelledWordLength](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/misspelledwordlength-property/llms-full.txt) | Returns the length of a misspelled word. |
| [MisspelledWordAtInputPos](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/misspelledwordatinputpos-property/llms-full.txt) | Returns the number of the misspelled word at the current input position. |
| [MisspelledWordDelete](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/misspelledworddelete-method/llms-full.txt) | Removes a word from the list of misspelled words and optionally replaces its text. |
| [Locale](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/locale-property/llms-full.txt) | Sets the language of the selected text. |

The programming category [Spell Checking](https://docs.textcontrol.com/tx-text-control-activex/reference/categories/spell-checking/llms-full.txt) lists all members of the spell checker interface.

### Related Pages

- [Selections, Clipboard and Undo](https://docs.textcontrol.com/tx-text-control-activex/document-editor/editing/llms-full.txt): selecting text in code with `SelStart`, `SelLength` and `SelText`.
- [Character and Paragraph Formatting](https://docs.textcontrol.com/tx-text-control-activex/document-editor/formatting/llms-full.txt): the character attributes of the selected text, which the language belongs to.
- [Version 16.0](https://docs.textcontrol.com/tx-text-control-activex/whats-new/older-versions/1600/llms-full.txt): the release that introduced the spell checker interface.
- [Spell Checking](https://docs.textcontrol.com/tx-text-control-activex/reference/categories/spell-checking/llms-full.txt): all members for spell checking.

---

## Nearby

- **Previous:** [Document Protection](https://docs.textcontrol.com/tx-text-control-activex/document-editor/protection/llms-full.txt)
- **Next:** [Selections, Clipboard and Undo](https://docs.textcontrol.com/tx-text-control-activex/document-editor/editing/llms-full.txt)
