// Invoked when the user chooses to fix the items found by the // Inspect operation. public void Fix(object Doc, int Hwnd, out Office.MsoDocInspectorStatus Status, out string Result) { Word.Document document = null; Word.StoryRanges storyRanges = null; Word.Range range = null; try { document = (Word.Document)Doc; storyRanges = document.StoryRanges; range = storyRanges[Word.WdStoryType.wdMainTextStory]; object find = null; object matchCase = false; object matchWord = true; object replaceWith = null; object matchWholeWord = false; object replace = Word.WdReplace.wdReplaceAll; object missing = Type.Missing; int i = 0; // Scan the whole document text and execute a search+replace // for all words that match our list of known UK spellings. foreach (string ukWord in ukWords) { find = ukWord; replaceWith = usWords[i]; range.Find.Execute( ref find, ref matchCase, ref matchWholeWord, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceWith, ref replace, ref missing, ref missing, ref missing, ref missing); i++; } Status = Office.MsoDocInspectorStatus.msoDocInspectorStatusDocOk; Result = "All UK words have been replaced."; } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.ToString()); Status = Office.MsoDocInspectorStatus.msoDocInspectorStatusError; Result = "Error."; } finally { storyRanges = null; range = null; document = null; // Note that if we force a GC collect here, it will hang Word. //GC.Collect(); //GC.WaitForPendingFinalizers(); //GC.Collect(); //GC.WaitForPendingFinalizers(); } }
// Invoked when the user chooses to go ahead and inspect the document. public void Inspect(object Doc, out Office.MsoDocInspectorStatus Status, out string Result, out string Action) { Word.Document document = null; Word.StoryRanges storyRanges = null; Word.Range range = null; try { document = (Word.Document)Doc; storyRanges = document.StoryRanges; // Build a list of words in the document that match the // known UK words in our list. ArrayList itemsFound = new ArrayList(); object find = null; object match = true; object missing = Type.Missing; foreach (string ukWord in this.ukWords) { find = ukWord; // We need to keep resetting the range to the whole document, // because Execute will reset it. range = storyRanges[Word.WdStoryType.wdMainTextStory]; if (range.Find.Execute( ref find, ref match, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing)) { itemsFound.Add(ukWord); } } if (itemsFound.Count > 0) { Status = Office.MsoDocInspectorStatus.msoDocInspectorStatusIssueFound; Result = String.Format("{0} UK words found.", itemsFound.Count); Action = "Replace UK Words"; } else { Status = Office.MsoDocInspectorStatus.msoDocInspectorStatusDocOk; Result = "No UK words found"; Action = "No UK words to remove"; } } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.ToString()); Status = Office.MsoDocInspectorStatus.msoDocInspectorStatusError; Result = "Error."; Action = "No action."; } finally { // This is important: if we don't clean up explicitly, // the Word process will not terminate. storyRanges = null; range = null; document = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } }