private void CheckWords(Word.Document doc, Hunspell hunspell, string selectedText, string[] khmerwords, out bool stopcheck, out bool repeatcheck, string objecttype = "", object wordobject = null) { int startposition = 0; Object oMissing = System.Reflection.Missing.Value; stopcheck = repeatcheck = false; //Check all the Khmer words from the selected line foreach (string khmerword in khmerwords) { DialogResult dialogResult = DialogResult.None; frmKhmer frmKhmer = null; String newKhmerWord = String.Empty; if (!hunspell.Spell(khmerword)) { if (!ignoreAllWords.Any(ignoreAllWord => ignoreAllWord.khmerword == khmerword)) { if (!ignoreWords.Contains(new IgnoreWord { document = doc.Name, khmerword = khmerword, selectedText = selectedText, startposition = startposition, ignoreAll = false })) { Word.Range start = null; Word.WdColorIndex highlightcolorindex = Word.WdColorIndex.wdNoHighlight; Word.WdUnderline fontunderline = Word.WdUnderline.wdUnderlineNone; Word.WdColor fontcolor = Word.WdColor.wdColorBlack; Word.Range selectionRange = null; //Select the erroneous word on the main document if (String.IsNullOrWhiteSpace(objecttype)) { //Set the initial selection start = doc.ActiveWindow.Selection.Range; //Set the search area doc.ActiveWindow.Selection.Start += startposition; Word.Selection searchArea = doc.ActiveWindow.Selection; //Set the find object Word.Find findObject = searchArea.Find; findObject.ClearFormatting(); findObject.Text = khmerword; //Find the mis-spelled word findObject.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); //Temp store the current formatting highlightcolorindex = doc.ActiveWindow.Selection.Range.HighlightColorIndex; fontunderline = doc.ActiveWindow.Selection.Range.Font.Underline; fontcolor = doc.ActiveWindow.Selection.Range.Font.UnderlineColor; //Highlight the selection doc.ActiveWindow.Selection.Range.HighlightColorIndex = Word.WdColorIndex.wdYellow; doc.ActiveWindow.Selection.Range.Font.Underline = Word.WdUnderline.wdUnderlineWavy; doc.ActiveWindow.Selection.Range.Font.UnderlineColor = Word.WdColor.wdColorRed; selectionRange = doc.ActiveWindow.Selection.Range; doc.ActiveWindow.Selection.Collapse(); } else { if (objecttype == "table") { start = ((Word.Cell)wordobject).Range; } else if (objecttype == "shape") { start = ((Word.Shape)wordobject).TextFrame.TextRange; start.Start += startposition; } //Set the find object Word.Find findObject = start.Find; findObject.ClearFormatting(); findObject.Text = khmerword; //Temp store the current formatting highlightcolorindex = start.HighlightColorIndex; fontunderline = start.Font.Underline; fontcolor = start.Font.UnderlineColor; //Find the mis-spelled word findObject.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); //Highlight the selection start.HighlightColorIndex = Word.WdColorIndex.wdYellow; start.Font.Underline = Word.WdUnderline.wdUnderlineWavy; start.Font.UnderlineColor = Word.WdColor.wdColorRed; start.Select(); } bool isObject = !String.IsNullOrWhiteSpace(objecttype); frmKhmer = new frmKhmer(selectedText, khmerword, startposition, hunspell.Suggest(khmerword), isObject); dialogResult = frmKhmer.ShowDialog(); //Select the line again if (String.IsNullOrWhiteSpace(objecttype)) { //Revert the highlights selectionRange.Select(); doc.ActiveWindow.Selection.Range.HighlightColorIndex = highlightcolorindex; doc.ActiveWindow.Selection.Range.Font.Underline = fontunderline; doc.ActiveWindow.Selection.Range.Font.UnderlineColor = fontcolor; if (dialogResult != DialogResult.Abort) start.Select(); } else { start.HighlightColorIndex = highlightcolorindex; start.Font.Underline = fontunderline; start.Font.UnderlineColor = fontcolor; if (dialogResult != DialogResult.Abort) { if (objecttype == "table") { ((Word.Cell)wordobject).Select(); } else if (objecttype == "shape") { ((Word.Shape)wordobject).Select(); } } } } } } #region Cancel Button Clicked //Return if the user hits Cancel Button if (dialogResult == DialogResult.Cancel || dialogResult == DialogResult.Abort) { stopcheck = true; repeatcheck = false; return; } #endregion #region Ignore or Ignore All Clicked //Ignore the word if (dialogResult == DialogResult.Ignore) { if (frmKhmer.ignoreAll) { ignoreAllWords.Add(new IgnoreWord { khmerword = khmerword, ignoreAll = frmKhmer.ignoreAll }); } else { ignoreWords.Add(new IgnoreWord { document = doc.Name, khmerword = khmerword, selectedText = selectedText, startposition = startposition }); } } #endregion #region Change or Change All Clicked if (dialogResult == DialogResult.Yes) { if (String.IsNullOrWhiteSpace(objecttype)) { //Set the initial selection Word.Range start = doc.ActiveWindow.Selection.Range; //Set the searcharea if (frmKhmer.changeAll) { doc.Content.Select(); } Word.Selection searchArea = doc.ActiveWindow.Selection; //Set the find object Word.Find findObject = searchArea.Find; findObject.ClearFormatting(); findObject.Text = khmerword; findObject.Replacement.ClearFormatting(); findObject.Replacement.Text = frmKhmer.selectedSuggestion; object replaceAll = frmKhmer.changeAll ? Word.WdReplace.wdReplaceAll : Word.WdReplace.wdReplaceOne; findObject.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing); newKhmerWord = frmKhmer.selectedSuggestion; //Set back the selection start.Select(); //Set repeatcheck to true if (frmKhmer.changeAll) { stopcheck = false; repeatcheck = true; return; } } else { var resultingText = selectedText.Replace(khmerword, frmKhmer.selectedSuggestion); if (objecttype == "table") { Word.Range range = ((Word.Cell)wordobject).Range; range.Text = resultingText; } else if (objecttype == "shape") { Word.Shape shape = (Word.Shape)wordobject; shape.TextFrame.TextRange.Text = resultingText; } stopcheck = false; repeatcheck = true; return; } } #endregion startposition += String.IsNullOrWhiteSpace(newKhmerWord) ? khmerword.Length : newKhmerWord.Length; } }
private void CheckWords(Word.Document doc, Hunspell hunspell, string selectedText, string[] khmerwords, out bool stopcheck, out bool repeatcheck, string objecttype = "", object wordobject = null) { int startposition = 0; Object oMissing = System.Reflection.Missing.Value; stopcheck = repeatcheck = false; //Check all the Khmer words from the selected line foreach (string khmerword in khmerwords) { DialogResult dialogResult = DialogResult.None; frmKhmer frmKhmer = null; String newKhmerWord = String.Empty; if (!hunspell.Spell(khmerword)) { if (!ignoreAllWords.Any(ignoreAllWord => ignoreAllWord.khmerword == khmerword)) { if (!ignoreWords.Contains(new IgnoreWord { document = doc.Name, khmerword = khmerword, selectedText = selectedText, startposition = startposition, ignoreAll = false })) { Word.Range start = null; Word.WdColorIndex highlightcolorindex = Word.WdColorIndex.wdNoHighlight; Word.WdUnderline fontunderline = Word.WdUnderline.wdUnderlineNone; Word.WdColor fontcolor = Word.WdColor.wdColorBlack; Word.Range selectionRange = null; //Select the erroneous word on the main document if (String.IsNullOrWhiteSpace(objecttype)) { //Set the initial selection start = doc.ActiveWindow.Selection.Range; //Set the search area doc.ActiveWindow.Selection.Start += startposition; Word.Selection searchArea = doc.ActiveWindow.Selection; //Set the find object Word.Find findObject = searchArea.Find; findObject.ClearFormatting(); findObject.Text = khmerword; //Find the mis-spelled word findObject.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); //Temp store the current formatting highlightcolorindex = doc.ActiveWindow.Selection.Range.HighlightColorIndex; fontunderline = doc.ActiveWindow.Selection.Range.Font.Underline; fontcolor = doc.ActiveWindow.Selection.Range.Font.UnderlineColor; //Highlight the selection doc.ActiveWindow.Selection.Range.HighlightColorIndex = Word.WdColorIndex.wdYellow; doc.ActiveWindow.Selection.Range.Font.Underline = Word.WdUnderline.wdUnderlineWavy; doc.ActiveWindow.Selection.Range.Font.UnderlineColor = Word.WdColor.wdColorRed; selectionRange = doc.ActiveWindow.Selection.Range; doc.ActiveWindow.Selection.Collapse(); } else { if (objecttype == "table") { start = ((Word.Cell)wordobject).Range; } else if (objecttype == "shape") { start = ((Word.Shape)wordobject).TextFrame.TextRange; start.Start += startposition; } //Set the find object Word.Find findObject = start.Find; findObject.ClearFormatting(); findObject.Text = khmerword; //Temp store the current formatting highlightcolorindex = start.HighlightColorIndex; fontunderline = start.Font.Underline; fontcolor = start.Font.UnderlineColor; //Find the mis-spelled word findObject.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); //Highlight the selection start.HighlightColorIndex = Word.WdColorIndex.wdYellow; start.Font.Underline = Word.WdUnderline.wdUnderlineWavy; start.Font.UnderlineColor = Word.WdColor.wdColorRed; start.Select(); } bool isObject = !String.IsNullOrWhiteSpace(objecttype); frmKhmer = new frmKhmer(selectedText, khmerword, startposition, hunspell.Suggest(khmerword), isObject); dialogResult = frmKhmer.ShowDialog(); //Select the line again if (String.IsNullOrWhiteSpace(objecttype)) { //Revert the highlights selectionRange.Select(); doc.ActiveWindow.Selection.Range.HighlightColorIndex = highlightcolorindex; doc.ActiveWindow.Selection.Range.Font.Underline = fontunderline; doc.ActiveWindow.Selection.Range.Font.UnderlineColor = fontcolor; if (dialogResult != DialogResult.Abort) { start.Select(); } } else { start.HighlightColorIndex = highlightcolorindex; start.Font.Underline = fontunderline; start.Font.UnderlineColor = fontcolor; if (dialogResult != DialogResult.Abort) { if (objecttype == "table") { ((Word.Cell)wordobject).Select(); } else if (objecttype == "shape") { ((Word.Shape)wordobject).Select(); } } } } } } #region Cancel Button Clicked //Return if the user hits Cancel Button if (dialogResult == DialogResult.Cancel || dialogResult == DialogResult.Abort) { stopcheck = true; repeatcheck = false; return; } #endregion #region Ignore or Ignore All Clicked //Ignore the word if (dialogResult == DialogResult.Ignore) { if (frmKhmer.ignoreAll) { ignoreAllWords.Add(new IgnoreWord { khmerword = khmerword, ignoreAll = frmKhmer.ignoreAll }); } else { ignoreWords.Add(new IgnoreWord { document = doc.Name, khmerword = khmerword, selectedText = selectedText, startposition = startposition }); } } #endregion #region Change or Change All Clicked if (dialogResult == DialogResult.Yes) { if (String.IsNullOrWhiteSpace(objecttype)) { //Set the initial selection Word.Range start = doc.ActiveWindow.Selection.Range; //Set the searcharea if (frmKhmer.changeAll) { doc.Content.Select(); } Word.Selection searchArea = doc.ActiveWindow.Selection; //Set the find object Word.Find findObject = searchArea.Find; findObject.ClearFormatting(); findObject.Text = khmerword; findObject.Replacement.ClearFormatting(); findObject.Replacement.Text = frmKhmer.selectedSuggestion; object replaceAll = frmKhmer.changeAll ? Word.WdReplace.wdReplaceAll : Word.WdReplace.wdReplaceOne; findObject.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref replaceAll, ref oMissing, ref oMissing, ref oMissing, ref oMissing); newKhmerWord = frmKhmer.selectedSuggestion; //Set back the selection start.Select(); //Set repeatcheck to true if (frmKhmer.changeAll) { stopcheck = false; repeatcheck = true; return; } } else { var resultingText = selectedText.Replace(khmerword, frmKhmer.selectedSuggestion); if (objecttype == "table") { Word.Range range = ((Word.Cell)wordobject).Range; range.Text = resultingText; } else if (objecttype == "shape") { Word.Shape shape = (Word.Shape)wordobject; shape.TextFrame.TextRange.Text = resultingText; } stopcheck = false; repeatcheck = true; return; } } #endregion startposition += String.IsNullOrWhiteSpace(newKhmerWord) ? khmerword.Length : newKhmerWord.Length; } }