在用word替换的功能时,一般的word都可以替换成功,但是word文本框里面的内容替换不到,经发现,当光标最后停留在文本框内的时候,就可以替换成功。
有没有办法解决无论光标在哪,都可以替换成功。求代码!

解决方案 »

  1.   

    可能没说清楚,word的替换功能,不能替换文本框里的内容,除非你的光标在文本框里面。
    其实也考虑过移动光标的方法,但是不知道怎样将光标移到文本框内.
      

  2.   


                object oMissing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word._Application oWord;
                Microsoft.Office.Interop.Word._Document oDoc;
                oWord = new Microsoft.Office.Interop.Word.Application();
                oWord.Visible = false;
                object fileName = "文件路径";
                oDoc = oWord.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);            object FindText, ReplaceWith, Replace;
                Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;            foreach (Microsoft.Office.Interop.Word.Shape shapex in oDoc.Shapes)
                {
                    shapex.Select(ref oMissing);                FindText = "old";//要查找的文本
                    ReplaceWith = "new";//替换文本
                    oWord.Selection.Find.Execute(ref FindText, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref ReplaceWith, ref Replace, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                }            oDoc.Save();            if (oDoc != null)
                    oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                if (oWord != null)
                    oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
      

  3.   

    执行 oWord.Selection.Find.Execute(ref FindText, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref ReplaceWith, ref Replace, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                报错 什么原因System.Runtime.InteropServices.COMException: 占位程序接收到错误数据