One thing that may make it even better, is adding support for subdirectories. (That is: Files in multiple subdirectories)
I am an absolute newbie in this, and don't know the syntax, the parameter, or where to add that.
Could somebody help?
如果您有幾十個包含相同內容(例如頁眉,頁腳,某些特殊單詞或數字)的Word文件,則需要在Word中的那些文檔中替換相同的內容。 您如何更輕鬆地快速完成工作? 當然,您可以一個個地打開這些文件以替換相同的內容,但這將很耗時且麻煩。 本教程將向您展示一種巧妙的方式來一次替換Word中多個文檔中的相同內容。
使用VBA代碼同時查找和替換多個Word文檔中的文本
使用Kutools for 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。 使用此功能,您可以輕鬆地同時在多個Word文檔中查找和替換不同的文本。 讓我們看看如何應用此功能來查找和替換文檔中的文本。
Kutools for Word :具有100多個方便的Word加載項, 60天免費試用.
1.請點擊 Kutools Plus > 批量查找和替換 啟用該功能。
2。 在裡面 批量查找和替換 對話框,請進行以下配置。
3。 關上 批量查找和替換 對話框
有關此功能的更多信息,請 點擊這裡....
如果您想免費試用該實用程序,請轉到 免費下載軟件 首先,然後按照上述步驟進行操作。