// 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(); } }
/// <summary> /// 替换指定文本在word中,doc和docx都支持 /// </summary> /// <param name="outFileName"></param> /// <param name="doc"></param> /// <param name="origialString"></param> /// <param name="destinationString"></param> /// <returns></returns> public static bool ReplaceString(string outFileName, word.Document doc, string origialString, string destinationString) { bool result = false; Object missing = System.Reflection.Missing.Value; try { object replaceAll = word.WdReplace.wdReplaceAll; _application.Selection.Find.ClearFormatting(); _application.Selection.Find.Text = origialString; _application.Selection.Find.Replacement.ClearFormatting(); _application.Selection.Find.Replacement.Text = destinationString; //设置替换的待替换的文本目标文本 _application.Selection.Find.Execute( ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing); // //替换在文本框中的文字 // word.StoryRanges ranges = _application.ActiveDocument.StoryRanges; foreach (word.Range item in ranges) { if (word.WdStoryType.wdTextFrameStory == item.StoryType) { word.Range range = item; while (range != null) { range.Find.ClearFormatting(); range.Find.Text = origialString; range.Find.Replacement.Text = destinationString; range.Find.Execute( ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing); range = range.NextStoryRange; } break; } } result = true; } catch { result = false; } finally { if (doc != null) { doc.SaveAs2(outFileName); doc.Close(true); doc = null; } if (_application != null) { _application.Quit(ref missing, ref missing, ref missing); _application = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return(result); }
/// <summary> /// 根据{1},{2}这种的占位符替换文本并保存到outFileName文件 /// </summary> /// <param name="outFileName"></param> /// <param name="doc"></param> /// <param name="origialString"></param> /// <param name="destinationString"></param> /// <param name="wait4ReplaceList"></param> /// <param name="removeRedundantPlaceHolder">是否去掉多余的placeholder</param> /// <param name="placeholdernum">placeholder的数量</param> /// <returns></returns> public static bool BatchReplaceStringByPlaceHolder(string outFileName, word.Document doc, List <string> wait4ReplaceList, bool removeRedundantPlaceHolder, int placeholdernum) { bool result = false; Object missing = System.Reflection.Missing.Value; object replaceAll = word.WdReplace.wdReplaceAll; try { for (int i = 0; i < placeholdernum; i++) { string src = "{" + (i + 1) + "}"; string dst; if (i >= wait4ReplaceList.Count && !removeRedundantPlaceHolder) { break; } if (i >= wait4ReplaceList.Count && removeRedundantPlaceHolder) { dst = ""; } else { dst = wait4ReplaceList[i]; } _application.Selection.Find.ClearFormatting(); _application.Selection.Find.Text = src; _application.Selection.Find.Replacement.ClearFormatting(); _application.Selection.Find.Replacement.Text = dst; //设置替换的待替换的文本目标文本 _application.Selection.Find.Execute( ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing); // //替换在文本框中的文字 // word.StoryRanges ranges = _application.ActiveDocument.StoryRanges; foreach (word.Range item in ranges) { if (word.WdStoryType.wdTextFrameStory == item.StoryType) { word.Range range = item; while (range != null) { range.Find.ClearFormatting(); range.Find.Text = src; range.Find.Replacement.Text = dst; range.Find.Execute( ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing); range = range.NextStoryRange; } break; } } result = true; } } catch { result = false; } finally { if (doc != null) { doc.SaveAs2(outFileName); doc.Close(true); doc = null; } if (_application != null) { _application.Quit(ref missing, ref missing, ref missing); _application = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return(result); }
// 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(); } }