Click here to Skip to main content
13,551,964 members
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 25 Jan 2008
Licenced CPOL

Tapping into the OpenOffice Spellchecker

, 25 Jan 2008
Rate this:
Please Sign up or sign in to vote.
A simple and inexpensive way to get your hands on a working spellchecker


There are a few articles, including some from Microsoft, explaining how to use Microsoft Office’s spellchecker in other applications through automation and .NET. However, the Microsoft Office suite is expensive, so here is a method using the spellchecker in OpenOffice 2.3.0. Microsoft Office provides a dialog box, but OpenOffice doesn't, so we have to make our own. Not a problem. This example is written with Visual C# 2008 Express.

Using the Code

The finished program won't start unless OpenOffice is installed on the target machine. The LoadSpellChecker() function does what’s necessary to load the modules into memory. mxSpell is a class member of type XSpellChecker.

private bool LoadSpellChecker()
        XComponentContext context = uno.util.Bootstrap.bootstrap();
        XMultiComponentFactory factory = context.getServiceManager();
        XLinguServiceManager lsm = 
        mxSpell = lsm.getSpellChecker();
    catch( ex)
    return mxSpell != null;

In the spellchecking function, the Regex breaks the text in the TextBox into words for checking, and each word is fed in turn into XSpellChecker.spell(). This returns null if there is no alternative spelling suggestion - we take that to mean a correctly spelt word. Otherwise it returns a list of possible alternatives.

private void btnCheck_Click(object sender, EventArgs e)
    int nCorrections = 0;
    PropertyValue[] aEmptyProps = new PropertyValue[0];
//  Locale aLocale = new Locale("en", "US", "");
    Locale aLocale = new Locale("en", "GB", "");

    Regex reg = new Regex("\\w+");
    MatchCollection words = reg.Matches(txtToCheck.Text);
    foreach(Match word in words)
        txtToCheck.Select(word.Index, word.Length);
        XSpellAlternatives xAlt = mxSpell.spell(word.Value, aLocale, aEmptyProps);
        if(xAlt == null)

        SpellCheckDlg dlg = new SpellCheckDlg();
        dlg.Word = word.Value;
        foreach(string str in xAlt.getAlternatives())

        if(dlg.ListBox.Items.Count > 0)
            dlg.EnableButton = true;
            dlg.ListBox.SelectedIndex = 0;

            case DialogResult.Yes:
                txtToCheck.SelectedText = dlg.Word;

            case DialogResult.No:

            case DialogResult.Cancel:
                goto Finish;

    msg.Text = string.Format("{0} error{1} corrected.", nCorrections,
                    nCorrections == 1 ? "" : "s");

These alternatives are loaded into the ListBox in SpellCheckDlg, so that the user can decide what to do: replace the word, ignore the correction or quit the spellchecker - according to what is returned by the dialog. nCorrections keeps a count of corrections made, and this is subsequently displayed in the Label msg.

Points of Interest

It is not necessary to use the OpenOffice SDK in order to compile, but if you want to read the DevelopersGuide.pdf, you'll probably have to download the SDK. You'll need references in Visual Studio to the following DLLs:

  • C:\Program Files\ 2.3\program\cli_uno.dll
  • C:\Program Files\ 2.3\program\assembly\cli_basetypes.dll
  • C:\Program Files\ 2.3\program\assembly\cli_cppuhelper.dll
  • C:\Program Files\ 2.3\program\assembly\cli_types.dll

Of course, OpenOffice may be installed at some other place on your computer.

It is quite easy and interesting to change the language of the spellcheck, by changing the Locale which is fed into spell().


  • 25th January, 2008: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gordon Tolley
Hong Kong Hong Kong
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralOpen Office Spell Checker for .NET - spell checking without Open Office and the Open Office SDK Pin
Thomas Maierhofer9-Sep-09 8:31
memberThomas Maierhofer9-Sep-09 8:31 
GeneralRe: Open Office Spell Checker for .NET - spell checking without Open Office and the Open Office SDK Pin
jimmiea2-Oct-09 3:18
memberjimmiea2-Oct-09 3:18 
GeneralRe: Open Office Spell Checker for .NET - spell checking without Open Office and the Open Office SDK Pin
Thomas Maierhofer10-Nov-09 5:32
memberThomas Maierhofer10-Nov-09 5:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180515.1 | Last Updated 25 Jan 2008
Article Copyright 2008 by Gordon Tolley
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid