问题描述
参考网上资源,写了一小段程序,用来替换指定文件夹下若干个word文档中的指定字段,程序运行没什么错误,但是当一个文档超过两页之后,此文档中的文字就不会被替换掉,而一页的文档中的会成功替换掉。检查了好长时间也不知道是什么问题,请教各位高手,帮帮我。。。privatevoidWordReplace(stringfilePath,stringstrOld,stringstrNew){Microsoft.Office.Interop.Word._Applicationapp=newMicrosoft.Office.Interop.Word.ApplicationClass();//newMicrosoft.Office.Interop.Word.ApplicationClass();objectnullobj=Type.Missing;objectfile=filePath;Microsoft.Office.Interop.Word._Documentdoc=app.Documents.Open(reffile,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj)asMicrosoft.Office.Interop.Word._Document;app.Selection.Find.ClearFormatting();app.Selection.Find.Replacement.ClearFormatting();app.Selection.Find.Text=strOld;app.Selection.Find.Replacement.Text=strNew;objectobjReplace=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;app.Selection.Find.Execute(refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refobjReplace,refnullobj,refnullobj,refnullobj,refnullobj);//清空Range对象//Microsoft.Office.Interop.Word.Rangerange=null;//保存doc.Save();doc.Close(refnullobj,refnullobj,refnullobj);app.Quit(refnullobj,refnullobj,refnullobj);}
解决方案
解决方案二:
打开的word文档,要不就是需要逐页查找替换,要不就是替换的时候是当前页还是全文替换。
解决方案三:
早上有时间,就根据你的代码进行测试。vs2010office2010word文档有4页,替换了一个四页都有的字符串,测试通过。