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

C#在word文档中替换字符串
      在文档中搜索和替换字符串,先在word文档中标记字符串,然后再搜索标记字符串并用新的字符串替换标记字符串.主要是先选择整个文档
,然后使用Find的Execute方法查找指定字符串并替换为相应字符串.
以下实现方式之一,使用文档(Document )对象的 Content 属性选择整个文档。
     ///<summary>
        /// 在word 中查找一个字符串直接替换所需要的文本
        /// </summary>
        /// <param name="strOldText">原文本</param>
        /// <param name="strNewText">新文本</param>
        /// <returns></returns>
        public bool Replace(string strOldText,string strNewText)
        {
            this.oDoc.Content.Find.Text = strOldText ;
            object FindText,  ReplaceWith, Replace ;//
            object MissingValue = Type.Missing;
            FindText = strOldText ;//要查找的文本
            ReplaceWith = strNewText ;//替换文本
               Replace = Word.WdReplace.wdReplaceAll ;/**//*wdReplaceAll - 替换找到的所有项。
                                                      * wdReplaceNone - 不替换找到的任何项。
                                                    * wdReplaceOne - 替换找到的第一项。
                                                    * */
            this.oDoc.Content.Find.ClearFormatting();//移除Find的搜索文本和段落格式设置
            if (this.oDoc.Content.Find.Execute(
                ref FindText,ref MissingValue,
                ref MissingValue,ref MissingValue,
                ref MissingValue,ref MissingValue,
                ref MissingValue,ref MissingValue,ref MissingValue,
                ref ReplaceWith,ref Replace,
                ref MissingValue,ref MissingValue,
                ref MissingValue,ref MissingValue))
            {
                return true ;
            }
            return false ;
           
        }说明:其中oDoc是一个word文档的Document对象.
此外还可以 运用Word Application 对象Selection的Find.
public bool SearchReplace(string strOldText,string strNewText)
        {
            object replaceAll = Word.WdReplace.wdReplaceAll;
            object missing = Type.Missing;
           
                //首先清除任何现有的格式设置选项,然后设置搜索字符串 strOldText。
            this.oWordApplic.Selection.Find.ClearFormatting();
            oWordApplic.Selection.Find.Text = strOldText;
            oWordApplic.Selection.Find.Replacement.ClearFormatting();
            oWordApplic.Selection.Find.Replacement.Text = strNewText;
            if (oWordApplic.Selection.Find.Execute(
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref replaceAll, ref missing, ref missing, ref missing, ref missing))
            {
                return true ;
            }
            return false ;
        }    注:oWordApplic是一个Word Application 对象
   Find.Execute 方法详细介绍请看文档:http://msdn2.microsoft.com/zh-cn/library/microsoft.office.interop.word.find.execute(en-
us,VS.80).aspx
 
当然也可以使用word文档的书签BookMark.使用 Bookmark 的 Range 属性可将文本插入占位符书签,以便能够在以后检索文本,或替换已包含
文本的书签中的文本。可以参考 http://msdn.microsoft.com/library/chs/default.asp?
url=/library/CHS/dv_wrcore/html/wrtskhowtoupdatebookmarktext.asp
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值