在Word 2000中查找替换

本文关键字:查找 替换 2000 Word | 更新日期: 2023-09-27 18:16:26

如何在word文档中使用c#进行查找和替换?例如,让我们把"Run"这个词的所有实例替换为"Ran"?

可能是String之类的。替换("运行","跑");但是执行得很好,但是没有做任何更改。

在Word 2000中查找替换

查找和替换方法:

public void FindAndReplaceWordText(ref Microsoft.Office.Interop.Word.Document wordDoc, string textToReplace, string newText)
{
    object missing = System.Type.Missing;
    foreach (Microsoft.Office.Interop.Word.Range tmpRange in wordDoc.StoryRanges)
    {
        //set the text to find and replace
        tmpRange.Find.Text = textToReplace;
        tmpRange.Find.Replacement.Text = newText;
        tmpRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
        //Declare an object to pass as a parameter that sets the Replace parameter to the "wdReplaceOne" enum.
        object replaceOne = Microsoft.Office.Interop.Word.WdReplace.wdReplaceOne;
        //Execute the Find and Replace
        tmpRange.Find.Execute(ref missing, ref missing, ref missing,
                              ref missing, ref missing, ref missing, ref missing,
                              ref missing, ref missing, ref missing, ref replaceOne,
                              ref missing, ref missing, ref missing, ref missing);
    }

调用方法

FindAndReplaceWordText(ref wordDoc, "Ran", "Run");

希望这对你有帮助!