C#在word文档中替换字符串

时间:2023-02-05 21:41:46
在文档中搜索和替换字符串,先在word文档中标记字符串,然后再搜索标记字符串并用新的字符串替换标记字符串.主要是先选择整个文档,然后使用Find的Execute方法查找指定字符串并替换为相应字符串.

以下实现方式之一,使用文档(Document )对象的 Content 属性选择整个文档。
///<summary>
C#在word文档中替换字符串                /// 在word 中查找一个字符串直接替换所需要的文本
C#在word文档中替换字符串                /// </summary>
C#在word文档中替换字符串                /// <param name="strOldText">原文本</param>
C#在word文档中替换字符串                /// <param name="strNewText">新文本</param>
C#在word文档中替换字符串                /// <returns></returns>
C#在word文档中替换字符串                public bool Replace(string strOldText,string strNewText)
C#在word文档中替换字符串                {
C#在word文档中替换字符串                        this.oDoc.Content.Find.Text = strOldText ;
C#在word文档中替换字符串                        object FindText,    ReplaceWith, Replace ;//    
C#在word文档中替换字符串                        object MissingValue = Type.Missing;    
C#在word文档中替换字符串                        FindText = strOldText ;//要查找的文本
C#在word文档中替换字符串                        ReplaceWith = strNewText ;//替换文本
C#在word文档中替换字符串                             Replace = Word.WdReplace.wdReplaceAll ;/**//*wdReplaceAll - 替换找到的所有项。
C#在word文档中替换字符串                                                                                                            * wdReplaceNone - 不替换找到的任何项。
C#在word文档中替换字符串                                                                                                        * wdReplaceOne - 替换找到的第一项。
C#在word文档中替换字符串                                                                                                        * */

C#在word文档中替换字符串                        this.oDoc.Content.Find.ClearFormatting();//移除Find的搜索文本和段落格式设置
C#在word文档中替换字符串                        if (this.oDoc.Content.Find.Execute(
C#在word文档中替换字符串                                ref FindText,ref MissingValue,
C#在word文档中替换字符串                                ref MissingValue,ref MissingValue,
C#在word文档中替换字符串                                ref MissingValue,ref MissingValue,
C#在word文档中替换字符串                                ref MissingValue,ref MissingValue,ref MissingValue,
C#在word文档中替换字符串                                ref ReplaceWith,ref Replace,
C#在word文档中替换字符串                                ref MissingValue,ref MissingValue,
C#在word文档中替换字符串                                ref MissingValue,ref MissingValue))
C#在word文档中替换字符串                        {
C#在word文档中替换字符串                                return true ;
C#在word文档中替换字符串                        }
C#在word文档中替换字符串                        return false ;
C#在word文档中替换字符串                        
C#在word文档中替换字符串                }
  说明:其中oDoc是一个word文档的Document对象.

 
此外还可以 运用Word Application 对象Selection的Find. C#在word文档中替换字符串public bool SearchReplace(string strOldText,string strNewText)
C#在word文档中替换字符串                {    
C#在word文档中替换字符串                        object replaceAll = Word.WdReplace.wdReplaceAll;    
C#在word文档中替换字符串                        object missing = Type.Missing;    
C#在word文档中替换字符串                        
C#在word文档中替换字符串                                //首先清除任何现有的格式设置选项,然后设置搜索字符串 strOldText。
C#在word文档中替换字符串                        this.oWordApplic.Selection.Find.ClearFormatting();    
C#在word文档中替换字符串                        oWordApplic.Selection.Find.Text = strOldText;    
C#在word文档中替换字符串
C#在word文档中替换字符串                        oWordApplic.Selection.Find.Replacement.ClearFormatting();    
C#在word文档中替换字符串                        oWordApplic.Selection.Find.Replacement.Text = strNewText;    
C#在word文档中替换字符串
C#在word文档中替换字符串                        if (oWordApplic.Selection.Find.Execute(
C#在word文档中替换字符串                                ref missing, ref missing, ref missing, ref missing, ref missing,    
C#在word文档中替换字符串                                ref missing, ref missing, ref missing, ref missing, ref missing,
C#在word文档中替换字符串                                ref replaceAll, ref missing, ref missing, ref missing, ref missing))
C#在word文档中替换字符串                        {
C#在word文档中替换字符串                                return true ;
C#在word文档中替换字符串                        }
C#在word文档中替换字符串                        return false ;
C#在word文档中替换字符串                }
注:oWordApplic是一个Word Application 对象

   Find.Execute 方法详细介绍请看文档:http://msdn2.microsoft.com/zh-cn/library/microsoft.office.interop.word.find.execute(en-us,VS.80).aspx