提示:其他語言是Google翻譯的。 你可以訪問 English 版本。
登入
x
or
x
x
註冊
x

or

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

如果您有幾十個包含相同內容(如頁眉,頁腳,某些特殊字詞或數字)的單詞文件,並且您需要在Word中的這些文檔中替換相同的內容。 你如何快速完成它? 當然,你可以逐一打開這些文件來替換相同的內容,但是這將是費時和麻煩的。 本教程將向您展示一種巧妙的方法,即一次在Word中的多個文檔中替換相同的內容。

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


輕鬆地將多個文檔合併/合併為一個文檔:

合併文件 實用程序 Kutools for Word 可以幫助您將多個文檔快速合併為一個文檔。 你只需要:

  • 選擇你將合併成一個文件;
  • 指定每個組合文檔之間的中斷;
  • 開始合併。 看截圖:

Kutools for Word:擁有超過100便利的Word插件,可以免費試用30時間不受限制。 下載並免費試用現在!

Kutools for Word: 添加100新的高級功能 到Word 2003 / 2007 / 2010 / 2013 / 2016 / 2019。
Office Tab:啟用 在Office中選項卡式編輯和瀏覽,就像Chrome,Firefox,IE 8 / 9 / 10一樣。
Classic Menu for Office: 帶回Office 2003的舊樣子 到Office 2007,2010,2013,2016和2019。

使用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”。


Office Tab - 在Word中選項卡式瀏覽,編輯和管理多個文檔:

Office選項卡將Web瀏覽器(如Google Chrome,Internet Explorer新版本和Firefox)中顯示的選項卡式界面帶入Microsoft Word。 它 是一個省時的工具,在你的工作中無可替代。 見下面的演示:

點擊免費試用Office Tab!




Kutools for Word

超過Word 100,2003,2007,2010,2013和2016的2019高級功能

拍攝-kutools-700  -  225-KTE

更多功能 | 免費下載 | 只有$ 39.00數百個函數

Say something here...
symbols left.
You are guest ( Sign Up? )
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.
    Tiago · 23 days ago
    Is it possible to replace in the headers of the word doc?
    • To post as a guest, your comment is unpublished.
      crystal · 20 days ago
      Hi Tiago,
      Please apply the below VBA code to include the content of headers and footers when finding and replacing texts in multiple documents at once.

      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"
      If MsgBox("Do you want to replace texts in headers and footers too? ", vbYesNo, "Kutools for Word") = vbYes Then
      ActiveWindow.View.SplitSpecial = wdPanePrimaryFooter
      Selection.Find.Execute Replace:=wdReplaceAll
      Application.Run macroname:="NEWMACROS"
      ActiveWindow.View.SplitSpecial = wdPanePrimaryHeader
      Selection.Find.Execute Replace:=wdReplaceAll
      Application.Run macroname:="NEWMACROS"
      End If
      ActiveDocument.Save
      ActiveWindow.Close
      Next
      Application.ScreenUpdating = True
      End With
      MsgBox "Operation end, please view", vbInformation
      End Sub
  • To post as a guest, your comment is unpublished.
    dsadasd · 23 days ago
    Is that possible to replace in the header of the word doc?
  • To post as a guest, your comment is unpublished.
    martinadam · 3 months ago
    Thanks for sharing this information with us.
    Word search and replace tool
  • To post as a guest, your comment is unpublished.
    nathalie · 5 months ago
    Works perfectly but is there a similar macro to change text in the header! because this does not work with this version!
    thanks
    • To post as a guest, your comment is unpublished.
      crystal · 20 days ago
      Hi,
      Please apply the below VBA code to include the content of headers and footers when finding and replacing texts in multiple documents at once.

      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"
      If MsgBox("Do you want to replace texts in headers and footers too? ", vbYesNo, "Kutools for Word") = vbYes Then
      ActiveWindow.View.SplitSpecial = wdPanePrimaryFooter
      Selection.Find.Execute Replace:=wdReplaceAll
      Application.Run macroname:="NEWMACROS"
      ActiveWindow.View.SplitSpecial = wdPanePrimaryHeader
      Selection.Find.Execute Replace:=wdReplaceAll
      Application.Run macroname:="NEWMACROS"
      End If
      ActiveDocument.Save
      ActiveWindow.Close
      Next
      Application.ScreenUpdating = True
      End With
      MsgBox "Operation end, please view", vbInformation
      End Sub
  • To post as a guest, your comment is unpublished.
    blds · 1 years ago
    Doesn't work for me on footers. e.g find FFSOSxxxx and replace with GGSOSxxxx, i can see this code is great for others pls help. :(