Word.Document wDoc = null;
Word.Application wApp = null;
SysFun.CreateWordDocument( filePath + @"\Bumph\Lawless.etog", ref wDoc, ref wApp);            if (wDoc.Content.Text.IndexOf("测试") > 0)
            {
                MessageBox.Show("找出来了!!");
            }
可以找word文档里面的文本,但不懂如何替换,
用一个str替换掉'测试'

解决方案 »

  1.   

    if (wDoc.Content.Text.IndexOf("测试") > 0)
                {wDoc.Content.Text.Replace("测试",str);
           
                               }
      

  2.   

    /// <summary>
    /// 功能:查找或替换Word文件内容
    /// </summary>
    /// <param name="WordPath">Word文件路径</param>
    /// <param name="findText">查找的内容</param>
    /// <param name="replaceWith">替换的内容</param>
    /// <param name="matchCase">是否区分大小写</param>
    /// <returns>找到内容返回True</returns>
    public bool FindWord(string WordPath,string findText,string replaceWith,bool matchCase)
    {
    object filepath=WordPath;
    object FindText=findText;
    object MatchCase=matchCase;
    object ReplaceWith=replaceWith;
    object Replace=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
    object oMissing=System.Reflection.Missing.Value;
    Microsoft.Office.Interop.Word.Application WordApp=new Microsoft.Office.Interop.Word.ApplicationClass();
    Microsoft.Office.Interop.Word.Document WordDoc= WordApp.Documents.Open(ref filepath, 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); 
    bool bit=false; if(WordApp.Selection.Find.Execute(ref FindText,ref MatchCase,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))
    {
    bit=true;
    WordDoc.Save();
    }
    Console.WriteLine(WordApp.Documents.Count.ToString());
    WordDoc.Close(ref oMissing,ref oMissing,ref oMissing);
    WordApp.Quit(ref oMissing,ref oMissing,ref oMissing);
    WordDoc=null;
    WordApp=null;
    return bit;
    }
      

  3.   

    word替换字符串后,样式都变了
    Microsoft.Office.Interop.Word.ApplicationClass app =new Microsoft.Office.Interop.Word.ApplicationClass();
    //Microsoft.Office.Interop.Word.Application  app = new Microsoft.Office.Interop.Word.a();
    object nullobj = System.Reflection.Missing.Value;
    object file = filePath;
    Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(
    ref file, ref nullobj, ref nullobj,
    ref nullobj, ref nullobj, ref nullobj,
    ref nullobj, ref nullobj, ref nullobj,
    ref nullobj, ref nullobj, ref nullobj,
    ref nullobj, ref nullobj, ref nullobj, ref nullobj);
     
    doc.Content.Text = doc.Content.Text.Replace(strOld, strNew);  
    // doc.Content.AutoFormat();
    // Microsoft.Office.Interop.Word.Range range = null;
    doc.Close(ref nullobj, ref nullobj, ref nullobj);
    app.Quit(ref nullobj, ref nullobj, ref nullobj);
      

  4.   

    wDoc.Content.Find.Execute(ref tempss, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref shuju, ref missing, ref missing, ref missing, ref missing, ref missing);
    才能 替换掉  word里面的东东