在文檔中搜索和替換字符串,先在word文檔中標記字符串,然後再搜索標記字符串並用新的字符串替換標記字符串.主要是先選擇整個文檔,然後使用Find的Execute方法查找指定字符串並替換爲相應字符串.
以下實現方式之一,使用文檔(Document )對象的 Content 屬性選擇整個文檔。
以下實現方式之一,使用文檔(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 ;
}
/// 在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 ;
}
{
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 ;
}
Find.Execute 方法詳細介紹請看文檔:http://msdn2.microsoft.com/zh-cn/library/microsoft.office.interop.word.find.execute(en-us,VS.80).aspx