提示:其他语言是Google翻译的。 你可以访问 English 版本。
登录
x
or
x
x
注册账户
x

or

如何在word中搜索和替换多个文件?

如果您有几十个包含相同内容(例如页眉,页脚,某些特殊字词或数字)的单词文件,并且您需要在Word中的这些文档中替换相同的内容。 你如何更快地完成它? 当然,您可以逐一打开这些文件来替换相同的内容,但这会很耗时且麻烦。 本教程将向您展示一种巧妙的方法,可以一次替换Word中多个文档中的相同内容。

使用VBA代码在多个Word文档中查找和替换文本


轻松地将多个文档合并/合并为一个文档:

Acker 合并文件 实用程序 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.
    sathena99 · 3 years ago
    Thanks for posting this! I've run a couple tests and gotten it to work for me; however, it's not working on text in headers and footers for some reason. Is there a fix for this?
  • To post as a guest, your comment is unpublished.
    WORDZ · 4 years ago
    Big THX 4 the code... but also have that prblm with the headers... no change

    Thanks in advance
  • To post as a guest, your comment is unpublished.
    Nilesh · 4 years ago
    I want to change the format of the replaces text. i.e. "Pizza" has to be replaced with "[i]Burger[/i]".
    How can it be achieved?
  • To post as a guest, your comment is unpublished.
    Judy · 4 years ago
    Hello,
    This worked great for the word in the body of the document but did not replace the same word in the header. Any ideas.

    Thanks
  • To post as a guest, your comment is unpublished.
    Dani · 4 years ago
    This worked perfectly and saved me so much time. Thank you!