using System; using System.IO; using Word = Microsoft.Office.Interop.Word; namespace SavePicture { public class CreateTemplate { /// <summary> /// 替换成指定字符串 /// </summary> /// <param name="wordPath">要替换的word全路径</param> /// <param name="oldText">替换的文字</param> /// <param name="newText">替换成的文字</param> /// <returns>是否替换成功</returns> public static bool ReplaceWord(string wordPath, string oldText, string newText) { try { object nothing = System.Reflection.Missing.Value; object format = Word.WdSaveFormat.wdFormatDocument; object srcFileName = wordPath; object wrap = Word.WdFindWrap.wdFindContinue; Word.ApplicationClass appWord = new Word.ApplicationClass(); Word.Document wordDocument = appWord.Documents.Open(ref srcFileName, ref format, 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, ref nothing); appWord.Selection.Find.ClearFormatting(); appWord.Selection.Find.Replacement.ClearFormatting(); object objReplace = Word.WdReplace.wdReplaceAll; appWord.Selection.Find.ClearFormatting(); appWord.Selection.Find.Replacement.ClearFormatting(); appWord.Selection.Find.Text = oldText; appWord.Selection.Find.Replacement.Text = newText; appWord.Selection.Find.Execute(ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref wrap, ref nothing, ref nothing, ref objReplace, ref nothing, ref nothing, ref nothing, ref nothing); wordDocument.Save(); wordDocument.Close(); return true; } catch (Exception) { return false; } } /// <summary> /// 替换word中指定文本为图片 /// </summary> /// <param name="wordPath">word全路径</param> /// <param name="textLabel">替换字符串</param> /// <param name="imagePath">图片全路径</param> /// <returns>是否替换成功</returns> public static bool ReplaceImage(string wordPath, string textLabel, string imagePath) { try { object nothing = System.Reflection.Missing.Value; object missing = Type.Missing; object SaveWithDocument = true; object LinkToFile = false; object format = Word.WdSaveFormat.wdFormatDocument; object link = false; object confirmConversion = false; object objReplace = Word.WdReplace.wdReplaceOne; object wrap = Word.WdFindWrap.wdFindContinue; object srcFileName = wordPath; Word.ApplicationClass appWord = new Word.ApplicationClass(); Word.Document wordDocument = appWord.Documents.Open(ref srcFileName, ref format, 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, ref nothing); //替换图片过程 appWord.Selection.Find.ClearFormatting(); appWord.Selection.Find.Replacement.ClearFormatting(); bool found; appWord.Selection.Find.Text = textLabel; appWord.Selection.Find.Replacement.Text = ""; if (File.Exists(imagePath)) { //替换成图片 do { found = appWord.Selection.Find.Execute(ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref wrap, ref nothing, ref nothing, ref objReplace, ref nothing, ref nothing, ref nothing, ref nothing); if (found) { appWord.Selection.InlineShapes.AddPicture(imagePath, ref LinkToFile, ref SaveWithDocument, ref missing); } } while (appWord.Selection.Find.Found == true); } wordDocument.Save(); wordDocument.Close(); return true; } catch (Exception) { return false; } } } }
转载于:https://www.cnblogs.com/y279336671/archive/2012/07/20/2600820.html