可乐加冰
快乐在于你的选择!
posts - 63,comments - 29,trackbacks - 1
      在文档中搜索和替换字符串,先在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
    
posted on 2006-04-05 15:49 可乐加冰 阅读(1545) 评论(3)  编辑 收藏 网摘

FeedBack:
2006-10-19 14:20 | lionel[匿名] [未注册用户]
替换页眉里的字符怎么来做呢?
  回复  引用    
#2楼 [楼主]
2006-10-27 10:08 | 可乐加冰      
@lionel[匿名]
以下是一种方式,当然,不是最好的方式
foreach(Word.Section section in this.oDoc.Sections)
{
foreach(Word.HeaderFooter headerFooter in section.Headers)
{
headerFooter.Range.Text = "新的页眉";
}
foreach(Word.HeaderFooter headerFooter in section.Footers)
{
headerFooter.Range.Text = "新的页脚";
}
}
  回复  引用  查看    




标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2006-04-06 14:36 编辑过
Google站内搜索

相关文章:

相关链接: