- This topic explains about, how to spell check text in TextBox or RichTextBox in C# programming.
- To spell check requires a reference to the Word object model.
- How to add Word Object model to your application: Select Project/Add Reference. Click the COM tab and select “Microsoft Word 12.0 Object Library” (or whatever version you have on your system).
- You have to add a Namespace to your application to spell check in C#:
|
1 |
using Microsoft.Office.Interop; |
- Tools required creating Spell Check: Button1 (Button1), TextBox1 (TextBox1)
How to spell check in C# – Working
- When you click the Spell Check button (Button1), the program creates a Word.Application object and sets its visible property to False.
- It uses that object’s Documents.Add method to create a new Document object.
- It gets a Range object representing the Document’s text and copies the text from TextBox1 into the Document.
- Next the program activates the Document and calls its Check Spelling method.
- When Check Spelling returns, the program copies the result back into TextBox1, removing any leading and trailing carriage returns and line feeds that Word added.
- Finally it closes the Document, telling it not to save the new text, and closes the Word server.
How to spell check in C# – Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
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; using Microsoft.Office.Interop; using System.Reflection; namespace WindowsFormsApplication1 { public partial class Form1 : Form { Microsoft.Office.Interop.Word.Application word_server; Microsoft.Office.Interop.Word.Document doc; Microsoft.Office.Interop.Word.Range rng; object Template = Type.Missing; object NewTemplate = Type.Missing; object DocumentType = Type.Missing; object Visible = false; object optional = Missing.Value; object SaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; object OriginalFormat = Type.Missing; object RouteDocument = Type.Missing; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { if ((TextBox1.Text.Length > 0)) { // Make a Word server object. word_server= new Microsoft.Office.Interop.Word.Application(); // Hide the server. word_server.Visible = false; doc = word_server.Documents.Add(ref Template,ref NewTemplate,ref DocumentType,ref Visible); // Make a Range to represent the Document. rng = doc.Content; // Copy the text into the Document. rng.Text = TextBox1.Text; // Activate the Document and call its CheckSpelling // method. doc.Activate(); doc.CheckSpelling(ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional); // Copy the results back into the TextBox, // trimming off trailing CR and LF characters. //char[] chars = {Convert.ToChar(const.vbCr),Convert.ToChar(const.vbLf)}; TextBox1.Text = doc.Content.Text.Trim (); // Close the Document, not saving changes. //doc.Close(); doc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument); // Close the Word server. word_server.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument); } } } } |
If you have any suggestions or doubts regarding spell check, please contact us…
How to spell check in C#







