跳到主要內容

如何在Word文檔中同時查找和替換多個單詞?

Word提供了“查找和替換”功能以查找單詞或短語的所有實例,並同時用新單詞替換它們。 但是,如果您想同時查找和替換不同的單詞,此內置函數將無濟於事。 在本文中,我們討論的是一種VBA方法,用於同時在Word文檔中查找和替換多個不同的單詞。

使用VBA代碼在Word中同時查找和替換多個單詞
借助驚人的功能輕鬆地在Word中同時查找和替換多個單詞


使用VBA代碼在Word中同時查找和替換多個單詞

請執行以下操作以在Word文檔中同時查找和替換多個單詞。

1.打開要查找的Word文檔,並同時替換多個單詞,然後按 其他 + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。

2。 在裡面 Microsoft Visual Basic for Applications 窗口中,單擊 插入 > 模塊。 然後將下面的VBA代碼複製到“模塊”窗口中。

VBA代碼:在Word中同時查找和替換多個單詞

Sub FindAndReplaceMultiItems()
'Update by ExtendOffice 2018/10/25
    Dim xFind As String
    Dim xReplace As String
    Dim xFindArr, xReplaceArr
    Dim I As Long
    Application.ScreenUpdating = False
    xFind = InputBox("Enter items to be found here,seperated by comma: ", "Kutools for Word")
    xReplace = InputBox("Enter new items here, seperated by comma: ", "Kutools for Word")
    xFindArr = Split(xFind, ",")
    xReplaceArr = Split(xReplace, ",")
    If UBound(xFindArr) <> UBound(xReplaceArr) Then
        MsgBox "Find and replace characters must be equal.", vbInformation, "Kutools for Word"
        Exit Sub
    End If
    For I = 0 To UBound(xFindArr)
        Selection.HomeKey Unit:=wdStory
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = xFindArr(I)
            .Replacement.Text = xReplaceArr(I)
            .Format = False
            .MatchWholeWord = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    Next
    Application.ScreenUpdating = True
End Sub

3。 按 F5 鍵來運行代碼。

4.在第一 Kutools for Word 對話框中,輸入將找到的多個單詞並將其替換到文本框中,並用逗號分隔它們,然後單擊 OK 按鈕。 看截圖:

5.在第二 Kutools for Word 對話框中,輸入要替換為的新單詞(這些單詞也需要用逗號分隔),然後單擊 OK 按鈕。

注意: 在這種情況下,所有 “ KTE” 在本文檔中將被替換為 “新”,“ KTO”“ KTW” 將被替換為 “測試”“完”。 請根據您的需要進行更改。


借助驚人的功能輕鬆地在Word中同時查找和替換多個單詞

批量查找和替換 的特點 Kutools for Word 可以幫助您輕鬆地在一個文檔中或同時在多個文檔中查找和替換不同的文本。

在應用此功能之前,請花幾分鐘 首先下載並安裝.

1.啟動Microsoft Word應用程序,單擊 Kutools 加 > 批量查找和替換.

2。 在裡面 批量查找和替換 窗口,請進行以下配置。

  • 2.1點擊 按鈕> 添加文件 or 添加文件夾 添加一個或多個文檔,您將在其中找到並替換多個單詞。
  • 2.2點擊 添加行 按鈕插入查找和替換字段。 如果要同時查找和替換三個不同的文本,請創建三行。
  • 2.3在每一行中,輸入要用新單詞替換的現有單詞。 發現 列,然後將新詞輸入到 更換 列。
  • 2.4指定 搜索類型 每行。
  • 2.5在 查找 列,選擇將查找和替換應用到的位置。 這包括 主要文件, 頁腳 在這個部分。 您可以根據需要選擇其中之一,其中兩個或全部。
  • 2.6。 點擊 更換 按鈕開始操作。 看截圖:

然後,同時在所選文檔中替換特定單詞。

小提示: 您可以通過在 重點特色 一列。

  如果您想免費試用(60天)此實用程序, 請點擊下載,然後按照上述步驟進行操作。

最佳辦公生產力工具

Kutools for Word - 透過 Over 提升您的文字體驗 100 顯著特點!

🤖 Kutools 人工智慧助手:用人工智慧改變你的寫作 - 生成內容  /  重寫文字  /  總結文件  /  查詢資料 基於文檔,全部在Word中

📘 文件掌握: 分頁  /  合併文件  /  以各種格式匯出選擇(PDF/TXT/DOC/HTML...)  /  批次轉換為PDF  /  將頁面匯出為圖像  /  一次列印多個文件...

內容編輯: 批量查找和替換 跨多個文件  /  調整所有圖片的大小  /  轉置表行和列  /  將表格轉換為文字...

🧹 輕鬆清潔: 移開 多餘的空間  /  分節符  /  所有標題  /  文本框  /  超鏈接  / 如需更多拆卸工具,請前往我們的 刪除組...

創意插入: 插入 千位分隔符  /  複選框  /  單選按鈕  /  QR Code  /  條碼  /  對角線表  /  公式標題  /  圖片說明  /  表格標題  /  多張圖片  / 發現更多 插入組...

🔍 精準選擇:精確定位 特定頁面  /    /  形狀  /  標題段落  / 增強導航功能 更多 選擇功能...

星級增強: 快速導航至任何位置  /  自動插入重複文字  /  在文檔視窗之間無縫切換  /  11 轉換工具...

???? 想嘗試這些功能嗎? Kutools for Word 提供了 60天免費試用,沒有任何限制! 🚀
 
Comments (23)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
The VBA script you provide here seems to work only for Latin characters; is there some workaround to get special characters to work.
I work with Sanskrit, which used the Devanagari script, similar to Hindi. (1) The VBA does not accept the Devanagai font nor does it find the Latin characters with diacriticals. For example: It fails miserably if I search for mūlādhāra (मूलाधार). It doesn't seem to recognize the ū or ā, for example, and the Devanagari is a complete loss.

Moreover, does the script recognize spaces between words? If not, how do I get them in there?

Thank you!
This comment was minimized by the moderator on the site
Chào bạn, có cách nào thay thế RẤT NHIỀU cụm từ bằng RẤT NHIỀU cụm từ khác (nhập từng cụm từ rất mất thời gian) bằng cách m lập một file trong đó có 2 cột, 1 cột là cụm từ nguồn và cột 2 là cụm từ đích được không? rất cám ơn bạn.
This comment was minimized by the moderator on the site
Hi Nguyễn Phúc Lâm,
I recommand you apply the Batch Find and Replace feature of Kutools for Word (the second method in this post) to solve this problem.
In the Find and Replace dialog box, after creating the two columns you need, you can save the current settings as a scenaria for future use.
https://www.extendoffice.com/images/stories/comments/comment-picture-zxm/find_and_replace.png
This comment was minimized by the moderator on the site
Cám ơn Crystal, cách này có thể áp dụng với một số lượng nhỏ các cụm từ, nếu với số lượng các cụm từ lớn (hơn 1000, hơn 10000.. cụm từ) thì rất khó để có thể nhập thủ công. Vậy có cách nào khác không bạn? Cám ơn bạn đã trả lời
This comment was minimized by the moderator on the site
Hi Nguyễn Phúc Lâm,
Temporarily unable to deal with this problem with VBA code as it is a bit complex. This feature will be considered for upgrade in the next release. Sorry for the inconvenience.
This comment was minimized by the moderator on the site
Thanks for the project, Could you make it the way we can also replace letters in words. For example if i want to change Dollar as DoLLar it does not function.
This comment was minimized by the moderator on the site
In the first instruction (Find And Replace Multiple Words At The Same Time In Word With VBA Code), it does not find instances where the word to be replaced falls in the middle of a word (For example, .com following a website name). Can this be modified to do so?
This comment was minimized by the moderator on the site
What if I'm trying to replace commas?
This comment was minimized by the moderator on the site
The VBA code can't help to replace commas. You can apply Kutools to achieve.
This comment was minimized by the moderator on the site
After hitting Replace it just goes to Preview and doesn't do anything further.
This comment was minimized by the moderator on the site
Hi Ajs,All required words have been successfully replaced at once after hitting the Replace button. It goes to the Preview tab to help you know how many words have been successfully replaced. After that, close the dialog box.
This comment was minimized by the moderator on the site
Hi! First, congratulations for your work: this macro is very useful and interesting! I would like, neverthless, you help me with one thing. I am a proofreader and would like the replacements would highlithed in green or red color. How can I do this? Is there a code line I could use?
This comment was minimized by the moderator on the site
Hi, The Batch Find and Replace feature of Kutools for Word can perfectly solve your problem, you can have a try.
This comment was minimized by the moderator on the site
Hi how can this macro be revised to take more key words? I have about 170 words that I wold like to find and replace
This comment was minimized by the moderator on the site
Hi JM,
After running the code, a Kutools for Excel dialog box will pop up, please enter the keywords you will find and separate them with commas.
This comment was minimized by the moderator on the site
How to find and select multiple words at the same time
This comment was minimized by the moderator on the site
Hi,
After running the code, a Kutools for Excel dialog box will pop up, please enter the keywords you will find and separate them with commas.
This comment was minimized by the moderator on the site
Hi, This works well with English Words. Now I am doing a document where I translate English to Gujarati. So, when I apply this, (Find English Words) and (Replace with Gujarati words), it does change but it appears like "???". Doesn't show the Gujarati word but just question marks? Any further help? Please.
This comment was minimized by the moderator on the site
I'm have a similar problem with Sanskrit, not only the Devanagari script but also the English/Latin diacriticals on the special characters like ā, ū, ṛ, ṁ, ṃ, etc. Any suggestions for a workable fix?
This comment was minimized by the moderator on the site
Hello Shailesh, facing the same issue. Did you find a solution to it. Am also trying to figure out how to change from Chinese to English.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations