Note: The other languages of the website are Google-translated. Back to English
登入  \/ 
x
or
x
註冊  \/ 
x

or

如何在Word中跨多個文件搜索和替換?

如果您有幾十個包含相同內容(例如頁眉,頁腳,某些特殊單詞或數字)的Word文件,則需要在Word中的那些文檔中替換相同的內容。 您如何更輕鬆地快速完成工作? 當然,您可以一個個地打開這些文件以替換相同的內容,但這將很耗時且麻煩。 本教程將向您展示一種巧妙的方式來一次替換Word中多個文檔中的相同內容。

使用VBA代碼同時查找和替換多個Word文檔中的文本
使用Kutools for Word輕鬆一次在多個文檔中查找和替換不同文本


使用VBA代碼同時查找和替換多個Word文檔中的文本

1。 按 其他 + F11 打開 Microsoft Visual Basic for Applications 窗口。

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

VBA代碼:一次在多個文檔中搜索和替換相同內容

Sub CommandButton1_Click()
'Updated by Extendoffice 20180625
Dim xFileDialog As FileDialog, GetStr(1 To 100) As String '100 files is the maximum applying this code
Dim xFindStr As String
Dim xReplaceStr As String
Dim xDoc As Document
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFilePicker)
With xFileDialog
    .Filters.Clear
    .Filters.Add "All WORD File ", "*.docx", 1
    .AllowMultiSelect = True
    i = 1
    If .Show = -1 Then
        For Each stiSelectedItem In .SelectedItems
            GetStr(i) = stiSelectedItem
            i = i + 1
        Next
        i = i - 1
    End If
    Application.ScreenUpdating = False
    xFindStr = InputBox("Find what:", "Kutools for Word", xFindStr)
    xReplaceStr = InputBox("Replace with:", "Kutools for Word", xReplaceStr)
    For j = 1 To i Step 1
        Set xDoc = Documents.Open(FileName:=GetStr(j), Visible:=True)
        Windows(GetStr(j)).Activate
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = xFindStr  'Find What
            .Replacement.Text = xReplaceStr  'Replace With
            .Forward = True
            .Wrap = wdFindAsk
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchByte = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
        Application.Run macroname:="NEWMACROS"
        ActiveDocument.Save
        ActiveWindow.Close
    Next
    Application.ScreenUpdating = True
End With
MsgBox "Operation end, please view", vbInformation
End Sub

3。 按 F5 鍵來運行代碼。

4.在開幕 瀏覽 窗口,請找到並選擇您將在其中找到並替換文本的文檔,然後單擊 OK 按鈕。 看截圖:

5.在第一 Kutools for Word 對話框中,將您將在各個文檔中找到的文本輸入到 查找內容 框,然後單擊 OK 按鈕。

6.在第二 Kutools for Word 對話框中,輸入將替換為的文本,然後單擊 OK 按鈕。

8。 點擊 OK 下一個按鈕 微軟Word 對話框以完成查找和替換。

在這種情況下,所選文檔中的所有單詞“ Word”將同時替換為“ Excel”。


使用Kutools for Word輕鬆一次在多個文檔中查找和替換不同文本

在這裡強烈推薦 批量查找和替換 的特點 Kutools for Word。 使用此功能,您可以輕鬆地同時在多個Word文檔中查找和替換不同的文本。 讓我們看看如何應用此功能來查找和替換文檔中的文本。

Kutools for Word :具有100多個方便的Word加載項, 60天免費試用.

1.請點擊 Kutools Plus > 批量查找和替換 啟用該功能。

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

  • 2.1)點擊 添加行 按鈕下 查找和替換 標籤;
  • 2.2)在創建的行字段中:
A.在下面的文本框中輸入您將找到的文本 發現
B.將您要替換為的文本輸入到 更換 柱;
C.指定搜索類型,在哪裡可以找到文本,並根據需要使用顏色突出顯示文本。 在這種情況下,我選擇 全字匹配, 主要文件 並從 搜索類型, 查找突出 列;
  • 2.3)重複步驟2.1和2.2,添加更多您將找到並替換的文本;
  • 2.4)點擊  按鈕在 文件類型 添加要在其中查找和替換文本的Word文檔的部分;
  • 2.5)點擊 更換 or 發現 按鈕。 看截圖:

筆記:
1.如果單擊 發現 按鈕,所有查找結果將顯示在 預覽結果 標籤。 預覽結果後,如果要替換所有文本,請單擊 查找和替換 標籤。 否則,請關閉對話框。

2.如果單擊 更換 按鈕,所有指定的文本將立即被新的文本替換,結果也將顯示在 預覽結果 標籤。

3.如果在步驟2中指定了突出顯示顏色,則所有替換的文本將以指定的顏色突出顯示,您可以在文檔中一目了然地找到它們。

3。 關上 批量查找和替換 對話框

有關此功能的更多信息,請 點擊這裡....

如果您想免費試用該實用程序,請轉到 免費下載軟件 首先,然後按照上述步驟進行操作。



推薦的Word生產力工具

 

Kutools For Word-超過100種Word高級功能,節省50%的時間

  • 複雜和重複的操作可以在幾秒鐘內完成一次處理。
  • 跨文件夾一次將多個圖像插入Word文檔。
  • 將文件夾中的多個Word文件合併並合併為所需順序。
  • 根據標題,分節符或其他條件將當前文檔拆分為單獨的文檔。
  • 在Doc和Docx之間,Docx和PDF之間轉換文件,用於常見轉換和選擇的工具集合等...

Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Jebidiah Springfield · 7 years ago
    Wow you are a saint for putting this macro together! I am listening to 'Epic Battle Music' on Songza because I am such an amatuer Macro-smith and it's the only way I can make it through the pain and agony of crafting programming spells.

    Anyway, my point I am going to make is a spent a lot of time on Friday troubleshooting the part where you actually put in the text you want to find and putting in the text you want to replace - I did not quite understand this part in terms of where I put my "find" word text and "replace" word text:

    [quote]Note:you can change the Text = "search”, Replacement.Text = "find” to specify the content to be searched and the content to be applied for replacement.
    [/quote]

    I read over this again today and I wish to respectfully clarify for other programming non-initiates - in the section of VBA where you have copied and pasted the original programming language, make sure you put an end ' after the [i]what [/i] and a ' after the [i]with[/i] (see example below) - in my example, I am looking for multiple documents to replace Pizza with Stromboli:

    .Text = "Pizza" 'Find What'
    .Replacement.Text = "Stromboli" 'Replace With'
  • To post as a guest, your comment is unpublished.
    James R · 7 years ago
    Could this VBA technique be adapted to work for Publisher files? For example by changing *.docx to *.pub ?
  • To post as a guest, your comment is unpublished.
    Patrick · 7 years ago
    When you run the macro, the "Open" window will open. Navigate to the folder that contains the files you need to make the change to, then click once on the first one, then, while holding down the "Ctrl" key, click on the other files also requiring the find/replace. If all files in the folder need the action, just press the "Ctrl" and "A" keys. Then click on "Open" or press the "Enter" key, and the macro will begin.

    I should warn you, as I've performed this dozens of times at work: The macro will result in the changes being made only to the *body* of the Word document; headers, footers, and title text/alt-text will be excluded. If you have text in the aforementioned portions that needs replacing, you will have to perform a standard find/replace across individual documents.
  • To post as a guest, your comment is unpublished.
    Alison Pepper · 7 years ago
    Yes I have the same question as i think this would be useful but want to tell it to only look in certain folders.
  • To post as a guest, your comment is unpublished.
    Bobette · 7 years ago
    Will it ask where the documents in question are located? Or do all the documents need to be open?
    • To post as a guest, your comment is unpublished.
      PatrickRsGhost · 6 years ago
      When you run it, by default it will open up the "Open File" dialogue window to "My Documents". Navigate to the folder that contains the Word files you wish to make the changes to. You can press "Ctrl" + "A" to select all files, or hold down the "Ctrl" key while selecting certain files. Click on "Open" or press "Enter" and watch the magic happen.