Word 2007 Spellchecker Functionality in C#


First add the reference from the com tab, and add Microsoft word 12.0 object library.

Code given here with some description:

spellchecker.gif

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {

        Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();

        public Form1()
        {
                      InitializeComponent();
                  // if you want to show genarated word file then remove comment form nxt line    
                 //     WordApp.Visible = true;
 
                      this.FormClosed += new FormClosedEventHandler(closed);
        }
        private void closed(object sender, FormClosedEventArgs e)
        {
            //     close the active document without asking for save
WordApp.Documents.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges);
 
              //     it will closes the word application
            WordApp.Quit(false);
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Microsoft.Office.Interop.Word.Range DRange;

            this.Text = "Creating Word...";

            //adding new document to word application
            WordApp.Documents.Add();

            //set title

            this.Text = "Genarating error list...";

            //get the range of activer document
            DRange = WordApp.ActiveDocument.Range();

            //insert textbox data after the content of range of active document
            DRange.InsertAfter(textBox1.Text);

            //createing object for error collection and store the all errors
            Microsoft.Office.Interop.Word.ProofreadingErrors SpellCollection = DRange.SpellingErrors;

            if (SpellCollection.Count > 0)
            {
                listBox1.Items.Clear();
                listBox2.Items.Clear();
                int iword = 0;
                string newWord = null;
                for (iword = 1; iword <= SpellCollection.Count; iword++)
                {
                    newWord = SpellCollection[iword].Text;

                        listBox1.Items.Add(newWord);

                }
            }
            this.Text = "spelling checker Demo";

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
             
//     here it will find the suggestions based on the selecteditem of listbox1

            Microsoft.Office.Interop.Word.SpellingSuggestions CorrectionsCollection;

            CorrectionsCollection = WordApp.GetSpellingSuggestions(listBox1.Text);
            listBox2.Items.Clear();
            if (CorrectionsCollection.Count > 0)
            {
                int iWord = 0;
                for (iWord = 1; iWord <= CorrectionsCollection.Count; iWord++)
                {
                    listBox2.Items.Add(CorrectionsCollection[iWord].Name);
                }
            }
            else
            {
                listBox2.Items.Add("No suggestions!");
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            // it will replace word
            try
            {
                if (listBox1.SelectedIndex >= 0 & listBox2.SelectedIndex >= 0)
                {

                    textBox1.Select(textBox1.Text.IndexOf(listBox1.SelectedItem.ToString()), listBox1.SelectedItem.ToString().Length);
                    textBox1.SelectedText = listBox2.SelectedItem.ToString();
                    listBox1.Items.Remove(listBox1.SelectedItem);
                    listBox2.Items.Clear();

                }
            }
            catch (Exception ex)
            {
                //handle exception
            }
        }
    }
}


This is my first program for word operation in c#.

If you have any suggestion, feel free to reply.

Thank you.

Up Next
    Ebook Download
    View all
    Learn
    View all