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

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

以下实现方式之一,使用文档(Document )对象的 Content 属性选择整个文档。
ExpandedBlockStart.gif ContractedBlock.gif     /// <summary>
InBlock.gif        
/// 在word 中查找一个字符串直接替换所需要的文本
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strOldText">原文本</param>
InBlock.gif        
/// <param name="strNewText">新文本</param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif          public   bool  Replace( string  strOldText, string  strNewText)
ExpandedBlockStart.gifContractedBlock.gif        
{
InBlock.gif            
this.oDoc.Content.Find.Text = strOldText ;
InBlock.gif            
object FindText,  ReplaceWith, Replace ;// 
InBlock.gif
            object MissingValue = Type.Missing; 
InBlock.gif            FindText 
= strOldText ;//要查找的文本
InBlock.gif
            ReplaceWith = strNewText ;//替换文本
               Replace = Word.WdReplace.wdReplaceAll ;/**//*wdReplaceAll - 替换找到的所有项。
                                                      * wdReplaceNone - 不替换找到的任何项。
InBlock.gif                                                    * wdReplaceOne - 替换找到的第一项。
ExpandedSubBlockEnd.gif                                                    * 
*/

InBlock.gif            
this.oDoc.Content.Find.ClearFormatting();//移除Find的搜索文本和段落格式设置
InBlock.gif
            if (this.oDoc.Content.Find.Execute(
InBlock.gif                
ref FindText,ref MissingValue,
InBlock.gif                
ref MissingValue,ref MissingValue,
InBlock.gif                
ref MissingValue,ref MissingValue,
InBlock.gif                
ref MissingValue,ref MissingValue,ref MissingValue,
InBlock.gif                
ref ReplaceWith,ref Replace,
InBlock.gif                
ref MissingValue,ref MissingValue,
InBlock.gif                
ref MissingValue,ref MissingValue))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
InBlock.gif                
return true ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return false ;
InBlock.gif            
ExpandedBlockEnd.gif        }
说明:其中oDoc是一个word文档的Document对象.

此外还可以 运用Word Application 对象Selection的Find.
None.gif public   bool  SearchReplace( string  strOldText, string  strNewText)
ExpandedBlockStart.gifContractedBlock.gif        

InBlock.gif            
object replaceAll = Word.WdReplace.wdReplaceAll; 
InBlock.gif            
object missing = Type.Missing; 
InBlock.gif            
                //首先清除任何现有的格式设置选项,然后设置搜索字符串 strOldText。
InBlock.gif            
this.oWordApplic.Selection.Find.ClearFormatting(); 
InBlock.gif            oWordApplic.Selection.Find.Text 
= strOldText; 
InBlock.gif
InBlock.gif            oWordApplic.Selection.Find.Replacement.ClearFormatting(); 
InBlock.gif            oWordApplic.Selection.Find.Replacement.Text 
= strNewText; 
InBlock.gif
InBlock.gif            
if (oWordApplic.Selection.Find.Execute(
InBlock.gif                
ref missing, ref missing, ref missing, ref missing, ref missing, 
InBlock.gif                
ref missing, ref missing, ref missing, ref missing, ref missing,
InBlock.gif                
ref replaceAll, ref missing, ref missing, ref missing, ref missing))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
InBlock.gif                
return true ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return false ;
ExpandedBlockEnd.gif        }
    注: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
    

转载于:https://www.cnblogs.com/jinglelin/archive/2006/04/05/367586.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值