private void chkSpell_Click(object sender, EventArgs e) { Word.Application app = new Word.Application(); int errors = 0; //don't show word doing all the work app.Visible = false; //setting variables that are equivalent to null. object template = Missing.Value; object newTemplate = Missing.Value; object documentType = Missing.Value; object visible = true; //initalize a document Word._Document doc1 = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible); //a counter token int i = 0; //a variable to hold the output string[] holder = Regex.Split(txtIssue.Text, "\r\n"); while (holder.Length > i) { doc1.Words.First.InsertBefore(holder[i]); Word.ProofreadingErrors spellErrorsColl = doc1.SpellingErrors; errors = spellErrorsColl.Count; object optional = Missing.Value; doc1.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); object first = 0; object last = doc1.Characters.Count - 1; holder[i] = doc1.Range(ref first, ref last).Text; Word.Range deleteme = doc1.Range(ref first, ref last); deleteme.Delete(ref optional, ref optional); i = i + 1; } txtIssue.Text = ""; foreach (string line in holder) { txtIssue.Text += line + "\r\n"; } object saveChanges = false; object originalFormat = Missing.Value; object routeDocument = Missing.Value; app.Quit(ref saveChanges, ref originalFormat, ref routeDocument); }
protected void btnCheck_Click(object sender, EventArgs e) { // Prevent multiple checker windows. if (applicationWord != null) { return; } applicationWord = new Microsoft.Office.Interop.Word.Application(); int errors = 0; if (tbInput.Text.Length > 0) { object template = Missing.Value; object newTemplate = Missing.Value; object documentType = Missing.Value; object visible = true; // Define a MS Word Document, then we use this document to calculate errors number and // invoke document's CheckSpelling method. Microsoft.Office.Interop.Word._Document documentCheck = applicationWord.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible); applicationWord.Visible = false; documentCheck.Words.First.InsertBefore(tbInput.Text); Microsoft.Office.Interop.Word.ProofreadingErrors spellErrorsColl = documentCheck.SpellingErrors; errors = spellErrorsColl.Count; object optional = Missing.Value; documentCheck.Activate(); documentCheck.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); documentCheck.LanguageDetected = true; // When users close the dialog, the error message will be displayed. if (errors == 0) { lbMessage.Text = "No errors"; } else { lbMessage.Text = "Total errors num:" + errors; } // Replace misspelled words of TextBox. object first = 0; object last = documentCheck.Characters.Count - 1; tbInput.Text = documentCheck.Range(ref first, ref last).Text; } object saveChanges = false; object originalFormat = Missing.Value; object routeDocument = Missing.Value; ((_Application)applicationWord).Quit(ref saveChanges, ref originalFormat, ref routeDocument); applicationWord = null; }
static void Main() { Console.WriteLine("Enter a string to spell-check:"); string stringToSpellCheck = Console.ReadLine(); string spellingResults; int errors = 0; if (stringToSpellCheck.Length == 0) { spellingResults = "No string to check"; } else { Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application(); Console.WriteLine("\nChecking the string for misspellings ..."); app.Visible = false; Microsoft.Office.Interop.Word._Document tempDoc = app.Documents.Add(); tempDoc.Words.First.InsertBefore(stringToSpellCheck); Microsoft.Office.Interop.Word.ProofreadingErrors spellErrorsColl = tempDoc.SpellingErrors; errors = spellErrorsColl.Count; //1. Before C# 4.0 //object ignoreCase = true; //object alwaysSuggest = false; //object optional = Missing.Value; //tempDoc.CheckSpelling( ref optional, ref ignoreCase, ref alwaysSuggest, // ref optional, ref optional, ref optional, ref optional, ref optional, // ref optional, ref optional, ref optional, ref optional ); //2. Using the "omit ref" feature of C# 4.0 object optional = Missing.Value; tempDoc.CheckSpelling( optional, true, false, optional, optional, optional, optional, optional, optional, optional, optional, optional); //3. Using "omit ref" and optional parameters //tempDoc.CheckSpelling( Missing.Value, true, false ); app.Quit(false); spellingResults = errors + " errors found"; } Console.WriteLine(spellingResults); Console.WriteLine("\nPress <Enter> to exit program."); Console.ReadLine(); }
private void button1_Click(object sender, System.EventArgs e) { var app = new Word.Application(); var errors = 0; if (textBox1.Text.Length > 0) { app.Visible = false; // Setting these variables is comparable to passing null to the function. // This is necessary because the C# null cannot be passed by reference. object template = Missing.Value; object newTemplate = Missing.Value; object documentType = Missing.Value; object visible = true; Word._Document doc1 = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible); doc1.Words.First.InsertBefore(textBox1.Text); Word.ProofreadingErrors spellErrorsColl = doc1.SpellingErrors; errors = spellErrorsColl.Count; object optional = Missing.Value; doc1.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); label1.Text = $"{errors} errors corrected "; object first = 0; object last = doc1.Characters.Count - 1; textBox1.Text = doc1.Range(ref first, ref last).Text; } object saveChanges = false; object originalFormat = Missing.Value; object routeDocument = Missing.Value; app.Quit(ref saveChanges, ref originalFormat, ref routeDocument); }