public static void FindString(object info) { ReplaceInFiles.ReplaceFileInfo replaceFileInfo = (ReplaceInFiles.ReplaceFileInfo)info; WordApp app = GetWordApplication(); foreach (string file in replaceFileInfo.files) { Word.Document doc = app.APP.Documents.Open(file, ref Nothing, true, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); int foundCount = FindText(doc, replaceFileInfo.searchText); replaceFileInfo.mainForm.SetRichBoxText(string.Format("在文件【{0}】中找到【{1}】处【{2}】。 ", Path.GetFileName(file), foundCount, replaceFileInfo.searchText)); doc.Saved = true; doc.Close(); } if (app.Type == WordAPPType.NEW) { app.APP.Quit(); } replaceFileInfo.mainForm.SetRichBoxText("查找结束!"); replaceFileInfo.mainForm.SetStatus(true); }
public static void ReplaceString(object info) { ReplaceInFiles.ReplaceFileInfo replaceFileInfo = (ReplaceInFiles.ReplaceFileInfo)info; WordApp app = GetWordApplication(); foreach (string file in replaceFileInfo.files) { Word.Document doc = app.APP.Documents.Open(file, ref Nothing, true, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); ReplaceAll(doc, replaceFileInfo.searchText, replaceFileInfo.replaceText); replaceFileInfo.mainForm.SetRichBoxText(string.Format("文件【{0}】替换成功。 ", Path.GetFileName(file))); string resultPath = Path.Combine(replaceFileInfo.resultFolder, Path.GetFileName(file)); doc.SaveAs2(resultPath); doc.Close(); } if (app.Type == WordAPPType.NEW) { app.APP.Quit(); } replaceFileInfo.mainForm.SetRichBoxText(string.Format("替换结束!共计替换【{0}】个文件", replaceFileInfo.files.Count)); replaceFileInfo.mainForm.SetStatus(true); }